gpt4 book ai didi

javascript - 如何在 Rails 中的 Controller 操作成功或失败的 View 中显示 JSON 状态消息?

转载 作者:行者123 更新时间:2023-12-03 12:07:59 25 4
gpt4 key购买 nike

我将某些数据从 View 发送到我的 Controller 。 Controller 操作检查用户是否有足够的钱,如果有,则允许他们购买特定价格的工具。否则,它不会更新他们的工具。无论哪种方式,我都想将 JSON 响应发送回 View 以进行显示。

如何显示这些消息?

这是我的 Controller :

# Updates dollars and tool id when a user goes shopping in 'store'...
def update_tool

@user = User.find(params[:user_id])
@tool = Tool.find(params[:toolId])
price = @tool.price

# subtract tool price from users dollars
if @user.dollars <= price

respond_to do |format|
msg = { :status => "error", :message => "You do not have enough money!", :html => "<b>NO MONEY!</b>" }
format.json { render :json => msg }
end

else

@user.dollars = @user.dollars - price

# re-assign users tool_id reference ID
@user.tool_id = @tool.id

#store to database
@user.save

#sends a confirmation back to store
respond_to do |format|
msg = { :status => "success", :message => "You purchased a tool!", :html => "..." }
format.json { render :json => msg }
end
end
end

我想获取这些状态响应并使用它们来触发我认为的事件,像这样的东西:

success: function(){
window.alert(':message');
},
error: function(){
window.alert(':message');
}

我只是不确定如何访问 json 响应消息的内容。

<小时/>

更新:

这是我的 AJAX 请求,以及我的成功或失败函数:

function buyTool() {

$.ajax({
type: 'PATCH',
headers: {
'X-CSRF-Token': '<%= form_authenticity_token.to_s %>'
},
url: '<%= update_tool_path %>',
dataType: "JSON",
async: true,
data: {
'user_id' : <%= @user.id %>,
'toolId' : toolId
},
success: function(){
window.alert(":json");
},
error: function(){
window.alert(":json");
}
});


};

它不起作用——我的警报窗口实际上只是显示文本“:json”。

我需要传递匿名错误:函数该数据吗?

最佳答案

我对此的偏好是使用由 ajax 触发的闪烁。为此,请使用以下命令。

将以下内容添加到您的 ApplicationController.rb

  after_filter :flash_to_headers
#....
private

def flash_to_headers
return unless request.xhr?
response.headers['X-Message'] = flash_message
response.headers["X-Message-Type"] = flash_type.to_s

flash.discard # don't want the flash to appear when you reload page
end

def flash_message
[:error, :warning, :notice, :success].each do |type|
return flash[type] unless flash[type].blank?
end
end

def flash_type
[:error, :warning, :notice, :success].each do |type|
return type unless flash[type].blank?
end
end

然后添加包含以下内容的 flashes.js.coffee 文件(这使用引导样式的 flashes,因此只需将类更改为具有您自己的样式的内容即可)

show_ajax_message = (msg, type) ->
if (type == "error")
$("#flash-message").html "<div id='flash-#{type}' class='alert alert-danger'>#{msg}</div>"
else if (type == "success")
$("#flash-message").html "<div id='flash-#{type}' class='alert alert-success'>#{msg}</div>"
else if (type == "notice")
$("#flash-message").html "<div id='flash-#{type}' class='alert alert-info'>#{msg}</div>"
else if (type == "warning")
$("#flash-message").html "<div id='flash-#{type}' class='alert alert-warning'>#{msg}</div>"
$("#flash-#{type}").delay(5000).slideUp 'slow'

$(document).ajaxComplete (event, request) ->
msg = request.getResponseHeader("X-Message")
type = request.getResponseHeader("X-Message-Type")
show_ajax_message msg, type

最后添加闪光渲染的地方

# views/shared/_flashes.html.erb
<!-- Id is used for ajax flashes -->
<div id="flash-message">
<% if flash[:notice] %>
<div class="alert alert-success">
<%= flash[:notice] %>
</div>
<% elsif flash[:error] %>
<div class="alert alert-danger">
<%= flash[:error] %>
</div>
<% elsif flash[:alert] %>
<div class="alert alert-info">
<%= flash[:alert] %>
</div>
<% end %>
<% flash.discard %>
</div>

并从您的layouts/application.html.erb渲染它

<%= render 'shared/flashes' %>

此后,您可以像通常在 Rails 中一样触发 Flash 消息,它们就会出现。

关于javascript - 如何在 Rails 中的 Controller 操作成功或失败的 View 中显示 JSON 状态消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25098980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com