【问题标题】:How to get Rails 3 link_to :partial to work如何让 Rails 3 link_to :部分工作
【发布时间】: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=&gt;"profile_c", :controller=&gt;"profiles", :id=&gt;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


    【解决方案1】:

    让你的路线像:

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

    【讨论】:

    • 还是得到No route matches {:action=&gt;"profile_c", :controller=&gt;"profiles", :id=&gt;1}
    • 如果你写 :on => :collection 而不是 :on => :member ?
    • 你的 :action=>"profile_questions" 在哪里?编辑您的问题以获取更多详细信息
    猜你喜欢
    • 2011-05-19
    • 1970-01-01
    • 2016-07-27
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多