Passing local rails to variable in javascript for partial

I give up my search, I usually try to figure these things out on my own, but I try my best and I just want it to work

I have a link as shown below where @puser is the user of the profile that I am currently viewing.

<%= link_to 'Request', new_or_edit_relationship_path(nil), :remote => true, :locals => { :puser => @puser} %> 

This in turn causes new_relationship_path, which is the .js.erb file below

 alert("<%= escape_javascript(puser.id) %>") 

Why is this not working !? He says the variable or method puser is undefined. This works great if I should just do a partial transfer to the locals, but no. Javascript does not want to play beautifully

Can someone explain why I or the program are stupid?

+1
ruby-on-rails-3
source share
1 answer

When you execute link_to as remote , the user starts a new request when clicking on the link. Therefore, passing local does not mean anything for a new request. ( local no longer exists in the new request.)

So, in order for @puser exist in the new request, you need to pass the id for this @puser via the URL (whatever you do for new_or_edit_relationship_path ). The new request should look for puser that id , and then it can use it in JS alert() .

Hope this helps and is a bit clearer than dirt.

+1
source share

All Articles