0

Rails 4 button_to

<%= button_to 'Send', {:controller => "feedback", :action => "send_feedback"}, class: "btn btn-success pull-right" %>

In API documentation i saw, that default behaviour for button_to is to send POST queries, but in log:

Started GET "/?name=%D0%98%D0%B3%D0%BE%D1%80%D1%8C&email=silend%40inbox.ru&text=ghfhfg&authenticity_token=MbPeULaBSsjRmf1415AUfwQOLl5%2FkpOUzbOyJnBRMXa7%2Fgi6yyoTn8kSsVR3sFEkVxMH%2FN%2FfDEFfwWpN%2Bjc5NA%3D%3D"

In routes.rb:

post "feedback/send_feedback" => "feedback#send_feedback"

Everything works fine when i add "formmethod = post" in result html inside browser, but how to change it with rails?

2 Answers2

1

You should set the method to use:

<%= button_to 'Send', {controller: "feedback", action: "send_feedback"}, method: :post, class: "btn btn-success pull-right" %>

see the docs here

For the example:

<%= button_to 'Save', {controller: "links", action: "create"}, method: :post, class: "btn btn-success pull-right" %>

this code was generated:

<form class="button_to" action="/links" method="post">
<input class="btn btn-success pull-right" type="submit" value="Save">
<input type="hidden" value="kT0h/RaXsLfOGouIYKS6Td767VF1+q72GFl6XIIwzcbd+/Au/7gvTeGRf5ajMMV9Ds3PO7U5bF4/O5zKgJWzqQ==" name="authenticity_token">
</form>

Also use the new Hash sintaxe, see What are the benefits of the new hash syntax in Ruby 1.9?

Community
  • 1
  • 1
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
0

Problem was with form, not button_to helper. I wrote path to my controller/action with method in form tag:

<%= form_tag("/feedback/send_feedback", method: "post", class: "form-horizontal") do %>

and change button_to to submit_tag

<%= submit_tag "Отправить", class: "btn btn-success pull-right" %>

And now it works