Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're trying to post a value to the server you should be using an HTML form. That's what they're designed for. In fact, when you specify <code>:method =&gt; :post</code> as on option for the <code>link_to</code> helper, Rails dynamically creates an HTML form that is immediately sent when the link is clicked. Why not just do this?</p> <pre><code>&lt;%= form_tag '/save_search' do -%&gt; &lt;%= text_field_tag 'search_name', :class =&gt; 'search_save' %&gt; &lt;%= submit_tag 'Save search' %&gt; &lt;% end -%&gt; </code></pre> <p>This will post the value to the server (accessible as <code>params[:search_name]</code> inside the controller action) like required.</p> <p>If its an issue of aesthetics (as in, you think buttons are ugly and want links instead) you can always style the submit button to look like a link with plain old CSS. To the user it will look like a link but more importantly it will behave like a button and submit the form without any additional logic.</p> <p>If you're still not convinced that using a form is the way to go and you're keen on making it work, you can do so with jQuery. Let's take your example code and modify it slightly</p> <pre><code>&lt;%= text_field_tag 'search_name', :class =&gt; 'search_save' %&gt; &lt;%= link_to "Save search" , save_search_path(@some_value), :id =&gt; 'save_search' %&gt; </code></pre> <p>Then inside your <code>application.js</code> you can add the following.</p> <pre><code>$(function() { // Add click event binding to `Save search` link $("#save_search").on("click", function(event) { event.preventDefault(); // don't trigger default // get the value inside the text field var name = $("#search_name").val(); $.post('/save_search', { search_name: name }, function(data) { // log the result from the server, or whatever... console.log(data); }); }); }); </code></pre> <p>This event will get fired when someone clicks on the "Save search" link and will make an AJAX call to the server with the specified data (unless the user did something silly like disable JavaScript). The controller action will be able to access this data via <code>params[:search_name]</code>. Hope this helps. Happy coding!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload