【发布时间】:2011-10-22 20:38:33
【问题描述】:
在我的 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
这是profiles_controller.rb 中的profile_c 操作:
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
还有我的链接到:
<li><%= link_to "Link 3", profile_c_profile_path(:id => @profile.id), :remote => true %></li>
【问题讨论】:
标签: jquery ruby-on-rails-3 partial-views