通过AJAX Rails的闪光的通知 [英] Rails flash notice via ajax
问题描述
长话短说,我有一个按钮。在单击它,我想一个Ajax请求触发它得到闪光[:通知],并在$一个div
Long story short, I have a button. On clicking it, I want an ajax request to be triggered which gets flash[:notice] and displays it in a div in$
下面是我的缩短的观点:
Here is my shortened view:
<input type="button" id="search" value="display"/>
<div id="notice">
</div>
在查看我的Ajax请求:
My ajax request in the view:
$("#search").submit(function(){
$.ajax({
type: "POST",
url: //url to my show action
success: function(data){
/*$("#notice").html("<%= flash[:notice] %>");
$("#content").html(data);*/
}
});
return false;
});
我的控制器:
My controller:
def HomeController < ActionController::Base
def index
end
def show
respond_to do |format|
format.js { flash[:notice] = "" + count.to_s + " results found for " + params[:query][:search_key] + "" }
end
#render :partial => 'search'
end
end
我show.js.erb
My show.js.erb
#app/views/dashboard_home/show.js.erb
$("#notice").html("<%=j flash[:notice] %>");
$("#content").html("<%=j render partial: "search" %>");
现在的问题是,当我点击按钮,会显示该通知的罚款。但是同样的通知,接下来点击仍然存在太多。搜索部分包含表请帮帮忙!
The problem is when I click on button, the notice is displayed fine. But the same notice persists on the next clicks too. The search partial contains the table Please help!
推荐答案
会话
在下次点击相同的通知,仍然存在太多
the same notice persists on the next clicks too
这是由闪存
正在存储在会话
的Rails 的变量:
This is caused by the flash
being stored in the session
variable of Rails:
闪光灯会议的一个特殊的一部分被清除,每个 请求。这意味着存储在那里的值将只在可用 下一个请求,这是用于使错误消息等是有用的。
The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for passing error messages etc.
您的问题是,因为我不认为阿贾克斯被视为一个新的请求(需要在此引用),数据将持续到下一次你通过HTTP请求。
The problem you have is that since I don't think ajax counts as a new request (need reference for this), the data will persist into the next time you request via HTTP.
-
修正
我将首先尝试这个办法:
I would initially try this:
def show
respond_to do |format|
format.js { flash[:notice] = "my secret number "+rand(0,5)+" !" }
end
end
您的主要问题是,你正在使用处理在JS闪存
变量再培训局
preprocessor。这是一个问题,因为它意味着你将无法使用资产precompile来帮助它工作。
The main problem you have is you're processing the flash
variable in your JS using the ERB
preprocessor. This is an issue as it means you won't be able to use asset precompile to help it work.
在看<一href="http://stackoverflow.com/questions/366311/how-do-you-handle-rails-flash-with-ajax-requests">this问题,为什么不尝试使用<一个href="http://www.what$c$ccraves.com/articles/2008/12/13/rails-flash-with-ajax"><$c$c>after_filter$c$c>回调,像这样的:
After looking at this question, why not try using the after_filter
callback, like this:
#app/controllers/home_controller.rb
Class Home < ActionController::Base
after_filter { flash.discard if request.xhr? }, only: :show
def show
respond_to do |format|
format.js { flash[:notice] = "my secret number "+rand(0,5)+" !" }
end
end
end
-
更新
您应该包括成功
在功能上的 show.js.erb
:
You should include the success
functionality in your show.js.erb
:
#app/views/home/show.js.erb
$("#notice").html("<%= flash[:notice] %>");
这意味着你可以删除整个 AJAX
打来的电话的application.js
,并更换了远程:真正的
为您搜索表单:
This means you can remove the whole ajax
call from the application.js
, and replace with the remote: true
for your search form:
#app/views/search/index.html.erb
<%= form_tag home_show_path, remote: true %>
这部作品的原因是因为当你使用 format.js
响应块,Rails会加载 [动作] .js.erb
文件在你的看法。考虑到这只是发生的在的行动已经完成,它等同于成功的阿贾克斯
的功能。
The reason this works is because when you use the format.js
respond block, Rails will load the [action].js.erb
file in your views. Considering this only happens after the action has been completed, it's equivalent to the success
function of your ajax.
通过这样做,你就可以从删除整个 AJAX
函数的的application.js
,并与UJS版本替换,上述
By doing this, you'll be able to remove the entire ajax
function from your application.js
, and replace with the UJS version, as described above
这篇关于通过AJAX Rails的闪光的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!