如何获得Rails 3 link_to:部分工作

时间:2011-10-22 20:38:33

标签: jquery ruby-on-rails-3 partial-views

在我的Rails应用中,我想在用户的个人资料中添加link_to一个:partial的链接,以便不会始终加载个人资料的某些部分。我查看过各种文章和问题(主要是this one),但我无法让link_to工作。当我加载Profiles#Show时,我当前收到路由错误。

以下是我的代码。我是初学者,所以如果有人能帮我弄清楚发生了什么,我会很感激。

<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 个答案:

答案 0 :(得分:1)

制作您的路线:

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