简而言之,我有一个按钮。点击它后,我想要触发一个 Ajax 请求,获取 flash[:notice] 并将其显示在一个 div 中。
这是我的简短视图:
<input type="button" id="search" value="display"/>
<div id="notice">
</div>
在视图中的我的 Ajax 请求:
$("#search").submit(function(){
$.ajax({
type: "POST",
url: //url to my show action
success: function(data){
/*$("#notice").html("<%= flash[:notice] %>");
$("#content").html(data);*/
}
});
return false;
});
我的控制器:
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文件
#app/views/dashboard_home/show.js.erb
$("#notice").html("<%=j flash[:notice] %>");
$("#content").html("<%=j render partial: "search" %>");
问题是当我点击按钮时,通知正确显示。但相同的通知也会在下一次点击时继续存在。搜索部分包含表格,请帮忙!
views/home
文件夹中创建show.js.erb
文件 - 这将使用ERB
预处理器加载该文件,从而允许你显示flash
消息。由于这个文件是独立于asset pipeline
的,所以在预编译资源时不会丢失 :) - Richard Peck