Rails 3 - How to send data on link_to :remote=>true?
Asked Answered
M

3

5

I am trying to figure out the new link_to in Rails 3 but i still don't get it In Rails 2 I do:

<%= link_to_remote "My Link",:url=>{:action=>:myaction},:with=>"'data='+$('#someField').attr('value')" %>

but with the new syntax in Rails 3 how should it be?

I'm trying something like

<%=link_to "My Link",{:action=>"myaction"},:with=>"'data='+$('#someField').attr('value');",:remote=>true%>

but I'm not getting the quantity params in the controller's action

Moujik answered 3/5, 2011 at 15:59 Comment(0)
M
2

They say its no longer supported in this answer on How to send Javascript Variable to a controller's action with the link_to helper?

Moujik answered 3/5, 2011 at 21:58 Comment(0)
C
5

Something like this will send a "data" parameter with the value = 10.

link_to "My Link", { :controller => 'myctrler', :action=>"myact", :data=> 10 }, :remote=>true

I've never seen/used the :with option before. I'm sorry I can't help on that one.

Carrillo answered 3/5, 2011 at 19:49 Comment(3)
Still don't get data in the params i just get {"controller"=>"myController", "action"=>"myAction"}Moujik
The first hash is entirely used for the url. See railsapi.com/doc/rails-v3.0.7/classes/ActionView/Helpers/…Carrillo
This one works and send the value 10 for the data parameter but it's not working for me i want to get a value from a field or a variable from javascript when the link is clicked and sen this to my controller's actionMoujik
M
2

They say its no longer supported in this answer on How to send Javascript Variable to a controller's action with the link_to helper?

Moujik answered 3/5, 2011 at 21:58 Comment(0)
U
0

you just need to add the variable with the value in the path url, for example:

<%= link_to "SEND DATA", "server_function?myData=10", remote: true %>

and if you need to send more than one parameter you must to use & for example ?myData=10&myOtherData=12, where the params are myData with the value of 10 and myOtherData with 12.

Unaunabated answered 3/4, 2012 at 23:9 Comment(1)
It'd be better if I only use <%=link_to "sen data", {:controller=>:my_controller, :action=>:my_action, :data=>10, :other_param => "foo"}%> but way i was trying to do here was sending javascript variables through a remote link and now that's not possible on rails 3 as it was on rails two using the with parameter.Moujik

© 2022 - 2024 — McMap. All rights reserved.