2012-07-16 153 views
2

在我的Rails 3.2.3應用程序中,我使用的是Ajax和jQuery。網頁上有一個鏈接,我想用一個按鈕替換它。下面的代碼工作完美Rails - link_to和button_to

<%= 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
<%= 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"> 
+0

這不起作用。 '@ current_page'在哪裏? – Alexandre 2012-07-16 16:54:39

+0

參數'id'在哪裏? – Alexandre 2012-07-16 17:16:19

+0

@AlexMaslakov,此上下文中的參數是頁面 – 2012-07-16 17:25:08

1

您必須更改方法:method=>:get,因爲默認情況下它設置爲post。和你的路線可能不是正確的路由,

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