Rails3 - How to send Javascript Variable to controller action using hel_link?

If I have the following javascript code

var myVar = 0;

function setNewValforVar(newValue){
    myVar = newValue;
}

and I call setNewValforVarthe ntimes function

so when I click the link, it sends the value myVarto the controller action in the remote link, for example

<%=link_to "My Link",{:action=>"myAction"},:data=>''sval='+myVar',:remote=>true%>

I Rails 2.3.x I would do it with

<%=link_to_remote "My Link",:url=>{:action=>"myAction"},:with=>"'sval='+myVar"%>

and I got it from the controller action using

params["myVar"]

How to do this on Rails 3 using the helper link_to?

+3
source share
2 answers

Rails 3 no longer supports this behavior. You will need to either create a form to submit the value, or change the hreflinks to include the value in the query string.

+4

, . - , , id:

<%=link_to "My Link", {:action=>"myAction"}, :remote=>true, :id => "mylink" %>

prototype_ujs: URL- ( , , ).

<%= javascript_tag("document.on('ajax:create', 'a#mylink',
      function(event) { event.memo.request.url += '&sval=' + myVar; })") %>

() id.

+3

All Articles