Search code examples
javascriptjqueryruby-on-railsfaye

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


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.


Solution

  • <%= 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 =.