rails 4带有块的js.erb文件:忽略其余的erb代码(rails 4 js.erb file with block: rest of erb code is ignored)

编程入门 行业动态 更新时间:2024-10-24 13:23:50
rails 4带有块的js.erb文件:忽略其余的erb代码(rails 4 js.erb file with block: rest of erb code is ignored)

我正在研究一个非常简单的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 end

Nothing 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 =.

更多推荐

本文发布于:2023-07-31 02:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   文件   js   erb   rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!