Rails 3 link_to 部分,内部有 for 循环
我以前从未编程过,并且很难让 link_to
在我的 Rails 3 应用程序内的用户个人资料中呈现相应的 :partial
。总共有三个部分:
- _profile_credits (为了这个问题,我主要关注
_profile_credits
。) - _profile_about
- _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 中链接的位置以及我希望加载部分信息的位置:
<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
操作中指定了更多内容。但是,如果我在其他人的个人资料上,则会加载我的积分,而不是我正在浏览其个人资料的用户的积分。谁能帮我解决这个问题吗?
I've never programmed before and am having difficulty getting link_to
to render the corresponding :partial
in a users' profile inside of my Rails 3 app. All in all there are three partials:
- _profile_credits (I'm focusing on
_profile_credits
for the sake of this question.) - _profile_about
- _profile_reviews
What I want to do is click the following:
<li><%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote => true %></li>
And have the following partial (_profile_credits) load and render each credit in @user.credits:
<% for credit in @user.credits %>
<div class="credit">
</div>
<% end %>
Where I'm trying to render the partial is beneath the links and inside of the div container. The HTML below shows the position of the links within the div and where I'd like the info from the partials to load:
<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 -->
And the rest of my code...
My profiles_controller#profile_credits
action:
def profile_credits
respond_to do |format|
format.js { render :layout => false }
end
end
In my routes.rb
:
resources :profiles do
get :profile_credits, :on => :member
end
My profile_credits.js.erb
:
$( "#tabs" ).html( "<%= escape_javascript( render (:partial => "profile_credits", :locals => { :id => @profile.id } ) ) %>" );
At the moment nothing happens when I click a link. I've tried following the various Rails 3 UJS examples but can't get it. At one point I specified more in the ProfilesController profile_credits
action. However, if I was on someone else's profile my credits were loading, not the credits of the user whose profile I was browsing. Can anyone help me figure this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于让这个工作了。给定一个包含
导航和用于加载内容的
的
,这是我的代码:
我的容器的 HTML:
_profile_credits.html.erb
部分:profile_credits.js.erb
:profile_credits.rb
控制器:就这样了。
I finally got this to work. Given a
<div>
that contains a<ul>
navigation and a<div>
for the loaded content, here is my code:HTML for my container:
_profile_credits.html.erb
partial:profile_credits.js.erb
:profile_credits.rb
controller:That did it.