Rails button_to发送获取查询而不是发布

时间:2015-09-06 10:00:24

标签: ruby-on-rails ruby

Rails 4 button_to

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

在我看到的API文档中,button_to的默认行为是发送POST查询,但是在日志中:

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"

在routes.rb中:

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

当我在浏览器中的结果html中添加“formmethod = post”时,一切正常,但是如何用rails更改它?

2 个答案:

答案 0 :(得分:1)

您应该设置要使用的方法:

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

查看文档here

例如:

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

生成了这段代码:

<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>

同样使用新的Hash sintaxe,请参阅What are the benefits of the new hash syntax in Ruby 1.9?

答案 1 :(得分:0)

问题出在表单上,​​而不是button_to帮助程序。 我使用表单标记中的方法编写了控制器/操作的路径:

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

并将button_to更改为submit_tag

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

现在可行了