2011-10-22 63 views
1

我从来没有编程过,并且难以让link_to在我的Rails 3应用程序内的用户配置文件中呈现相应的:partial。所有的一切有三个谐音:Rails 3 link_to部分与for循环内

  1. _profile_credits(我专注于_profile_credits对这个问题的缘故
  2. _profile_about
  3. _profile_reviews

我想什么请点击以下链接:

<li><%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote => true %></li> 

,并具有以下部分(_profile_credits)负载和@ user.credits呈现每个信用:

<% for credit in @user.credits %> 
    <div class="credit"> 
    </div> 
<% end %> 

当我试图渲染部分是链接下方,在div容器内。下面的HTML显示专区内的链接的位置和我想从谐音来负载信息:

<div id="tabs"> 
    <ul id="infoContainer"> 
    <li><%= link_to "Reviews", profile_reviews_profile_path(:id => @profile.id), :remote => true %></li> 
    <li><%= link_to "About", profile_about_profile_path(:id => @profile.id), :remote => true %></li> 
    <li><%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote => true %></li> 
    </ul> 
    <div id="tabs-1"> 
    ##load information from the partials inside `<div id="tabs-1"> 
    </div> 
</div><!-- end tabs --> 

和我的代码的其余部分...

profiles_controller#profile_credits行动:

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

在我routes.rb

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

profile_credits.js.erb

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

目前没有任何反应,当我点击一个链接。我试过了各种Rails 3 UJS的例子,但无法得到它。在一个点上,我在ProfilesController profile_credits操作中指定了更多。但是,如果我在其他人的个人资料中加载了我的点数,而不是我浏览的个人资料的点数。任何人都可以帮我解决这个问题吗?

+2

既然你说你从来没有编程过,我会提出来。不要将任意变量名用于任何其他非常简单的目的(例如''作为迭代器)。部分原因尚未得到解答很可能是因为任何人都很难猜测给定的命名为'a'的命令和命名为'c'的命令。这些是什么? – numbers1311407

+1

哎呀,他们是学分。我为了保持我的URL更短而命名,但可能不应该让我指出我如何命名控制器操作和部分。 – tvalent2

+0

我已将完整的动作名称放入以供参考。谢谢你的提示。 – tvalent2

回答

0

我终于得到了这个工作。给定一个<div>包含<ul>导航和加载的内容<div>,这里是我的代码:

HTML我的容器:

<div id="tabs"> 
    <ul id="infoContainer"> 
    <li><%= link_to "Reviews", profile_reviews_profile_path, :remote => true %></li> 
    <li><%= link_to "About", profile_about_profile_path, :remote => true %></li> 
    <li><%= link_to "Credits", profile_credits_profile_path, :remote => true %></li> 
    </ul> 
    <div id="tabs-1"> 
    <%= render :partial 'profile_reviews %> #default 
    </div> 
</div><!-- end tabs --> 

_profile_credits.html.erb部分:

<% for credit in @user.credits %> 
    <div class="credit"> 
    </div> 
<% end %> 

profile_credits.js.erb

$("#tabs-1").html("<%= escape_javascript(render (:partial => "profile_credits")) %>"); 

profile_credits.rb控制器:

def profile_credits 
    @profile = Profile.find(params[:id]) 
    @user = User.find(@profile.user_id) 
    @credits = User.find(@profile.id).credits 
    respond_to do |format| 
    format.html { render :layout => nil } 
    format.xml 
    format.js { render :layout => nil } 
    end 
end 

这做到了。