0

I have a Rails site where creating a new post responds to JavaScript.

I'm also using the private_pub gem and trigger a push message after creating the post.

The user should be redirected to the post after it has been created.

This is the content of create.js.erb:

<%= @users.each do |user| %>
  <% publish_to "/user/#{user.id}" do %>
    $("#messages").prepend("<%= j render(@message) %>");
  <% end %>
<% end %>

var url = "/posts/<%= j @post.id %>";
$(location).attr('href',url);

The push message is working fine, but the user is not redirected.

If I remove the each-loop, the redirection works. Any other code is not influenced by the loop at all, also the position in the file doesn't change anything. I've tried many different approaches but haven't found any solution.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
TimN
  • 87
  • 1
  • 1
  • 8
  • Could you check the actual javascript as it is rendered on the browser? Could you paste the result with/without the loop? – Uri Agassi Nov 29 '14 at 14:15
  • @UriAgassi With the loop it returns the array and then the redirect, without it's just the redirect. Basically with the loop it looks like this: [#<User id: 2, ...;] var url = "/posts/3; $(location).attr('href',url); – TimN Nov 29 '14 at 14:33

1 Answers1

0

You need to remove the = from the <%= @users.each ...:

<% @users.each do |user| %>
  <% publish_to "/user/#{user.id}" do %>
    $("#messages").prepend("<%= j render(@message) %>");
  <% end %>
<% end %>

var url = "/posts/<%= j @post.id %>";
$(location).attr('href',url);

otherwise it will render the return value of the each, which is the array itself (@users)

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93