Rails - link_to和button_to

时间:2012-07-16 16:38:25

标签: ruby-on-rails ajax

在我的Rails 3.2.3应用程序中,我正在使用Ajax和jQuery。页面上有一个链接,我想用一个按钮替换它。下面的代码是完美的

<%= link_to "More", {:controller => "home", :action => "test_method", :page=>@current_page }, :remote => true,:id => 'lnk_more' %>

但这个不是

<%= button_to "More", {:controller => "home", :action => "test_method", :page=>@current_page }, :remote => true,:id => 'lnk_more' %>

链接和按钮的结果html在这里

#link
<a href="/home/test_method?page=1" data-remote="true" id="lnk_more" disabled="disabled">More </a>


#button
<form action="/home/test_method?page=1" class="button_to" data-remote="true" method="post"><div><input id="lnk_more" type="submit" value="More "><input name="authenticity_token" type="hidden" value="hFCuBR+88FYKEvNTZok+QRhxf6fHA+ucC6i2yc9hBEk="></div></form>

我做错了什么?

3 个答案:

答案 0 :(得分:3)

<%= button_to "More", 
     { :controller => :home, :action=> :test_method, :page => @current_page}, 
     { :remote => true, :id => 'lnk_more' } 
 %>

将为输入

提供ID
<input id="lnk_more" type="submit" value="More">

答案 1 :(得分:1)

您必须更改方法:method=>:get,因为默认设置为post。你的路线可能没有正确路由

<%= button_to "More", {:controller => "home", :action => "test_method", :page=>@current_page }, :method=>:get, :remote => true,:id => 'lnk_more' %>

答案 2 :(得分:0)

根据文档,remote选项应该是options的一部分。见http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to