我正在研究一个非常简单的RoR4论坛类应用程序。 这是用于创建新论坛的forums_controller.rb操作:
def create @new_forum = Forum.new(forum_params) if @new_forum.save respond_to do |format| format.html { redirect_to forums_path } format.js end else respond_to do |format| format.html { render 'new' } format.js { render partial: 'form_errors' } end end end没有什么魔法。 create js.erb看起来像这样:
<%= add_gritter("This is a notification just for you!") %> <%= broadcast "/forums/new" do %> $('#forum_form').remove(); $('#new_forum_link').show(); $('#forums_table').append('<%= j render @new_forum %>') <% end %>一切都很好。 您可以看到的广播块调用使用Faye来广播管理界面中的更改的javascript。
我担心的是,js.erb文件中的块之外的任何内容(即add_gritter调用)都被完全忽略。 我确定这不是一个喋喋不休的问题,因为改变那条线路进行简单的警报('hi'); 什么也没做。 但是,如果调用在块内部移动则会被执行(具有令人讨厌的效果,即每个客户端都会收到通知,警报或其他内容)。 我真的没有关于这个的想法,并将感谢任何帮助。
I'm working on a very simple RoR4 forum-like application. This is the forums_controller.rb actions for creating a new forum:
def create @new_forum = Forum.new(forum_params) if @new_forum.save respond_to do |format| format.html { redirect_to forums_path } format.js end else respond_to do |format| format.html { render 'new' } format.js { render partial: 'form_errors' } end end endNothing magic there. The create js.erb looks like this:
<%= add_gritter("This is a notification just for you!") %> <%= broadcast "/forums/new" do %> $('#forum_form').remove(); $('#new_forum_link').show(); $('#forums_table').append('<%= j render @new_forum %>') <% end %>Everything is actually working fine. The broadcast block call you can see uses Faye to broadcast the javascript managing the changes in the interface.
My concern is, whatever is outside of the block in the js.erb file (i.e. that add_gritter call) is totally ignored. I'm sure it's not a gritter problem because changing that line for a simple alert('hi'); does nothing. However, if the call is moved inside the block it gets executed (with the annoying effect that every client gets the notification, alert or whatever). I am really out of ideas regarding this, and would appreciate any help.
最满意答案
<%= broadcast "/forums/new" do %>正在吐出执行的结果为javascript(即HTTOK),这使得它变得简单。 所以它应该是
<% broadcast "/forums/new" do %>注意缺少=。
<%= broadcast "/forums/new" do %>is spitting the result of the execution as javascript (that HTTOK) which makes it go meh. So it should be
<% broadcast "/forums/new" do %>Note the lack of the =.
更多推荐
发布评论