简体   繁体   English

带有块的rails 4 js.erb文件:其余的erb代码被忽略

[英]rails 4 js.erb file with block: rest of erb code is ignored

I'm working on a very simple RoR4 forum-like application. 我正在开发一个非常简单的类似RoR4论坛的应用程序。 This is the forums_controller.rb actions for creating a new forum: 这是用于创建新论坛的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

Nothing magic there. 那里没有魔术。 The create js.erb looks like this: 创建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 %>

Everything is actually working fine. 一切实际上都正常。 The broadcast block call you can see uses Faye to broadcast the javascript managing the changes in the interface. 您可以看到的广播块调用使用Faye广播管理界面更改的javascript。

My concern is, whatever is outside of the block in the js.erb file (ie that add_gritter call) is totally ignored. 我担心的是,js.erb文件中的块之外的任何内容(即add_gritter调用)都将被完全忽略。 I'm sure it's not a gritter problem because changing that line for a simple alert('hi'); 我确定这不是一个麻烦的问题,因为更改该行以生成简单的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 %>

is spitting the result of the execution as javascript (that HTTOK) which makes it go meh. 正在将执行结果作为javascript(那个HTTOK)吐痰,让它继续前进。 So it should be 所以应该

<% broadcast "/forums/new" do %>

Note the lack of the =. 请注意缺少=。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM