2011-10-22 70 views
1

在我的Rails應用程序中,我希望在用戶的個人資料中包含鏈接link_to a :partial,以便不總是加載配置文件的某些部分。我看了各種文章和問題(主要是this one),但我無法讓我的link_to工作。當我加載配置文件#顯示時,我目前收到路由錯誤。如何獲得Rails 3 link_to:部分工作

以下是我的代碼。我是初學者,所以如果有人能幫我弄清楚發生了什麼,我會很感激。

<div id="tabs"> 
    <ul id="infoContainer"> 
    <li><%= link_to "Link 1", {:partial => 'a'}, {:class => "a"}, :remote => true %></li> 
    <li><%= link_to "Link 2", {:partial => 'b'}, {:class => "a"}, :remote => true %></li> 
    <li><%= link_to "Link 3", profile_profile_c_path(:id => @user.id), :remote => true %></li> 
    </ul> 
    <div id="tabs-1"> 
    # I want to load the partials in this div 
    </div> 
</div><!-- end tabs --> 

我使用的最後link_to爲例:

<li><%= link_to "Link 3", profile_profile_c_path(:id => @user.id), :remote => true %></li> 

這裏是我的`的routes.rb文件:

match "/profiles/:id/c" => "profiles#profile_c" 
resources :profiles do 
    get :profile_c 
end 

這裏是在profile_c行動profiles_controller.rb

def profile_c 
    respond_to do |format| 
    format.js { render :layout => false } 
    end 
end 

這裏是我的profile_c.js.erb文件:

$("#tabs").html("<%= escape_javascript(render (:partial => "c", :locals => { :id => @user.id })) %>"); 

這是我_c.html.erb部分:

<% for object in @user.objects %> 
<div> 
</div> 
<% end %> 

錯誤顯示No route matches {:action=>"profile_c", :controller=>"profiles", :id=>1}

更新:我不再有錯誤。我改變了我的routes.rb到:

resources :profiles do 
    get :profile_c, :on => :member 
end 

而我的link_to到:

<li><%= link_to "Link 3", profile_c_profile_path(:id => @profile.id), :remote => true %></li> 

回答

1

作出這樣的路線:

resources :profiles do 
    get :profile_c, :on => :member 
end 
+0

是它的工作? –

+0

仍然得到'沒有路線匹配{:action =>「profile_c」,:controller =>「profiles」,:id => 1}' – tvalent2

+0

好吧,如果你寫:on =>:集合而不是:on =>:member ? –