简体   繁体   English

Rails-link_to和button_to

[英]Rails - link_to and button_to

In my Rails 3.2.3 application I'm using Ajax and jQuery. 在我的Rails 3.2.3应用程序中,我正在使用Ajax和jQuery。 There is a link on a page and I want to replace it with a button. 页面上有一个链接,我想用一个按钮替换它。 The code below is working perfect 下面的代码工作完美

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

But this one doesn't 但这不是

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

The result html for a link and for a button is here 链接和按钮的结果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>

What have I done wrong? 我做错了什么?

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

will give an id to the input 将给输入一个id

<input id="lnk_more" type="submit" value="More">

You must change the method :method=>:get , since by default it is set to post . 您必须更改方法:method=>:get ,因为默认情况下它设置为post And your routes are probably not routing it correctly 而且您的路线可能未正确路由

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

According to the documentation the remote option should be part of options . 根据文档, remote选项应该是options一部分。 See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to 参见http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM