Link_to if POST needed is a a hard nut to crack

So, I’m pretty much a beginner, so I would appreciate some help with this issue.

So, I have link_to helper (yes, I know that button_to is much easer, but for the needs of application it should be link_to), which should POST something in database without rederecting to any pages.

<div class = 'source> </blockquote> </blockquote> <blockquote> <blockquote> <p><%= link_to(result_array[k], :controller =>‘tests’, :action => ‘add_key’, :method => :post, :keyword => result_array[k], :position=>k, :remote =>true) + result_array[k+1]%></p> </blockquote> </blockquote> <blockquote> <blockquote> </div> </blockquote> </blockquote> <blockquote></blockquote> <p>This is a classic: rails thinks in this case that :method and :remote are parameters for your controller action, rather than options to apply to the link generation process.</p> <p>You need something like link_to(result_array[k], {:controller => ‘tests’, …}, :method => :post, :remote => true)</p> <p>And you also need the rails javascript loaded so that it will add javascript handlers to the links that create and submit a form on the fly</p> <blockquote> <p>t.</p> </blockquote> <blockquote></blockquote> <blockquote> <p>Kinda weird, because I don’t need any forms here and the second trouble that Key exist only like a Model, I didn’t created controller or view for Key because generally I don’t need them.</p> </blockquote> <blockquote></blockquote> <blockquote> <p>So I added form:</p> </blockquote> <blockquote></blockquote> <blockquote> <blockquote> <p><% form_for add_key_test_path do |f| %></p> </blockquote> </blockquote> <blockquote> <blockquote> <pre><code> <div class="actions"> </code></pre> </blockquote> </blockquote> <blockquote> <blockquote> <pre><code> <%= f.submit %> </code></pre> </blockquote> </blockquote> <blockquote> <blockquote> <pre><code> </div> </code></pre> </blockquote> </blockquote> <blockquote> <blockquote> <p><% end %></p> </blockquote> </blockquote> <blockquote></blockquote> <blockquote> <p>and add_key.js.erb to manage autosubmitting:</p> </blockquote> <blockquote></blockquote> <blockquote> <blockquote> <p>$(‘.source’).click(function(){</p> </blockquote> </blockquote> <blockquote> <blockquote> <pre><code>$(this).$('.actions').submit()

});

The thing you call submit on is the form, so something along the lines of

$(‘.source’).click(function() {

$(‘selector that will find the form’).submit()

})

Fred

Thaanks! finally works.

After an hours I tried to set up this quite hard stuff, it just working looks like a magic +)

once again rails guides for the rescue

chapter 1.2 Multiple Hashes in Form Helper Calls

“As with the link_to helper, the path argument doesn’t have to be given a string; it can be a hash of URL parameters recognizable by Rails’ routing mechanism, which will turn the hash into a validURL. However, since both arguments to form_tag are hashes, you can easily run into a problem if you would like to specify both. For instance, let’s say you write this:”

“Here, method and class are appended to the query string of the generated URL because you even though you mean to write two hashes, you really only specified one. So you need to tell Ruby which is which by delimiting the first hash (or both) with curly brackets. This will generate the HTMLyou expect:”