1

I'm giving up my search, I normally try to figure these things out on my own but I'm struggling hard and I just want this to work

I have the link_to seen below where @puser is the user of the profile I'm currently viewing.

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

This in turn calls new_relationship_path which is a .js.erb file seen below

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

Why won't this work!? It's saying the puser variable or method is undefined. This works perfect if I was to just render a partial passing in the locals but no. Javascript doesn't want to play nice

Could anyone help explain why me or the program is stupid?

TomMElack
  • 23
  • 6

1 Answers1

1

When you do a link_to as remote, the user is starting an entirely new request when they click the link. So passing a local means nothing to the new request. (The local doesn't exist any more on the new request.)

So in order for the @puser to exist on the new request, you need to pass the id for that @puser via the URL (whatever you have going on for new_or_edit_relationship_path). The new request needs to look up the puser by that id, and then it can use it in the JS alert().

Hope that helps and is a little clearer than mud.

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • Amazing, thank you for an incredibly fast response! It does make sense now. The controller ultimately is controlling the js and html file, right? It seems wrong to me though to load /relationships/new/1 where 1 is the user_id ... no? – TomMElack Sep 09 '12 at 19:18
  • Honestly, I haven't done much with remote helpers, so hopefully someone else can answer that question. I would probably do something like `/relationships?user[id]=1`. Ideally, you want a non-JS fallback that works with JavaScript turned off too. – Chris Peters Sep 09 '12 at 19:21
  • I guess as sub question ... why even bother letting me pass a locals when the call is a remote call. Why wouldn't it just error out if it's just going to drop what I'm passing in? – TomMElack Sep 09 '12 at 19:22
  • `link_to` is more than likely just ignoring your `locals` argument. Not sure when you would use it, but there may be something that I'm unaware of. – Chris Peters Sep 09 '12 at 19:23