如何获得Rails 3 link_to:部分工作 [英] How to get Rails 3 link_to :partial to work

查看:88
本文介绍了如何获得Rails 3 link_to:部分工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,我希望在用户的配置文件中有链接 link_to a :partial 以便确定部分配置文件并不总是加载。我看过各种文章和问题(主要是这个一个)但我不能让我的 link_to 工作。当我加载Profiles#Show时,我当前收到路由错误。

In my Rails app I want to have links in a user's Profile that link_to a :partial so that certain parts of the Profile aren't always loaded. I've looked at various articles and questions (mainly this one) but I can't get my link_to working. I currently get a routing error when I load Profiles#Show.

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

Below is my code. I'm a beginner so if anyone can help me figure out what's going on I'd appreciate it.

<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 例如:

I'm using the last link_to as an example:

<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 操作:

Here is the profile_c action in profiles_controller.rb:

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

这是我的 profile_c.js.erb 文件:

Here is my profile_c.js.erb file:

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

这是我的 _c.html.erb partial:

Here is my _c.html.erb partial:

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

错误显示没有路线匹配{:action =>profile_c ,:controller =>profiles,:id => 1}

更新:我没有更长时间出错。我将我的routes.rb更改为:

UPDATE: I no longer get an error. I changed my routes.rb to:

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>


推荐答案

让您的路线如下:

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

这篇关于如何获得Rails 3 link_to:部分工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆