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

Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85

3 Answers3

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.

Vijay Dev
  • 26,966
  • 21
  • 76
  • 96
  • Still don't get `data` in the params i just get `{"controller"=>"myController", "action"=>"myAction"}` – Mr_Nizzle May 03 '11 at 19:56
  • 1
    The first hash is entirely used for the url. See http://railsapi.com/doc/rails-v3.0.7/classes/ActionView/Helpers/UrlHelper.html#M006564 – Vijay Dev May 03 '11 at 20:21
  • 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 action – Mr_Nizzle May 03 '11 at 20:25
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?

Community
  • 1
  • 1
Mr_Nizzle
  • 6,644
  • 12
  • 55
  • 85
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.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
rome3ro
  • 131
  • 1
  • 6
  • 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. – Mr_Nizzle Apr 03 '12 at 23:33