Forum: Ruby on Rails Link_to if POST needed is a a hard nut to crack

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E1f877eccccc9e714b4d4fe03917f2a7?d=identicon&s=25 Barry (Guest)
on 2013-03-09 11:44
(Received via mailing list)
So, I'm pretty much a beginner, so I would appreciate some help with

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>
<%= link_to(result_array[k], :controller =>'tests',  :action =>
:method => :post, :keyword => result_array[k], :position=>k, :remote
=>true) + result_array[k+1]%>

add_key method in tests_controller.rb:

  def add_key
   @key.keyword = params[:keyword]
   @key.position = params[:position]
   respond_to do |format|

But despite I added :method=>:post option in server log I still see
"GET" request.

After an hour of hard googling I found out, that without form submitting
link_to won't create a "POST" request.

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
Key because generally I don't need them.

So I added form:

 <% form_for add_key_test_path do |f| %>
          <div class="actions">
           <%= f.submit %>
  <% end %>

and add_key.js.erb to manage autosubmitting:


But this doensn't work. I spent a 2-3 hours trying to get this finally
done, but can't find mistake or wrong point. Any advice or help would be
really appreciated.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2013-03-09 12:49
(Received via mailing list)
On Saturday, March 9, 2013 7:05:45 AM UTC, Barry wrote:
> :method => :post, :keyword => result_array[k], :position=>k, :remote
> =>true) + result_array[k+1]%>
> </div>
> 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
link generation process.

You need something like link_to(result_array[k], {:controller =>
...}, :method => :post, :remote => true)

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

>            <%= f.submit %>
The thing you call submit on is the form, so something along the lines

$('.source').click(function() {
  $('selector that will find the form').submit()

E1f877eccccc9e714b4d4fe03917f2a7?d=identicon&s=25 Barry (Guest)
on 2013-03-09 15:15
(Received via mailing list)
Thaanks! finally works.

After an hours I tried to set up this quite hard stuff, it just working
looks like a magic +)
5251fb12ad96bacd1f57e579b28b30fe?d=identicon&s=25 unknown (Guest)
on 2013-03-10 23:10
(Received via mailing list)
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 doesnt 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
would like to specify both. For instance, lets say you write this:"

"Here, method and class are appended to the query string of the
URL because you even though you mean to write two hashes, you really
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
This topic is locked and can not be replied to.