我有一个网站使用不引人注目的ajax。
简而言之,流动是:
我希望我的所有js.erb视图都能执行类似的“维护活动”,比如在需要时插入闪存消息,更新url等等。
我怎么才能把这个弄干?理想情况下,每次执行代码时,我都会使用分部,但我找不到如何做到这一点。
我的js.erb文件:
$("#challenge_actions").html("<%= escape_javascript(render partial: "challenges/best_practice_button")%>");
$("#flash_messages").html("<div id='flash_notice'><%= escape_javascript(flash[:notice])%></div>");
如何使用flash_messages提取行并将其自动包含在所有js.erb文件中?
发布于 2012-10-16 02:29:21
您可以使用js布局,就像html布局一样。
layouts/custom.js.erb
alert("<%=j flash[:notice] %>");
<%= yield %>
controllers/your_controller.rb
def your_action
flash[:notice] = "hello world"
@hello = "hello world"
respond_to do |format|
format.js { render layout: "custom" }
end
end
your_action.js.erb
alert("<%= j @hello %>");
发布于 2012-10-16 02:19:57
您也可以在erb中使用部分模板,假设您希望用id“容器”替换div的内容,然后假设您使用的是jquery,您可以这样做。
$('#container').html('<%= escape_javascript(render(:partial => 'mypartial'))%>')
https://stackoverflow.com/questions/12912320
复制相似问题