2015-09-06 209 views
0

轨道4 button_toRails的button_to发送GET查询,而不是后

<%= 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”,但如何改变它与铁路?

回答

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

而现在它的工作原理

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> 

也使用新的哈希sintaxe,请参阅What are the benefits of the new hash syntax in Ruby 1.9?

+0

谢谢你的回答,但与方法:后语法错误,方法:“后” - 仍然是GET查询。还试图使用旧的语法,如:方法=>:发布仍然相同 –

+0

错误与建议的语法是 - ActionView :: Template :: Error(未定义局部变量或方法'后'为#<#:0x007fc7d2bdbdd8 >) –

+0

@TorryAshwood我不明白,把'post'改为':post'。我编辑了答案。 –

相关问题