Rails 3 link_to 部分,内部有 for 循环

发布于 2024-12-11 16:21:04 字数 2052 浏览 1 评论 0原文

我以前从未编程过,并且很难让 link_to 在我的 Rails 3 应用程序内的用户个人资料中呈现相应的 :partial 。总共有三个部分:

  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 中链接的位置以及我希望加载部分信息的位置:

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

  1. _profile_credits (I'm focusing on _profile_credits for the sake of this question.)
  2. _profile_about
  3. _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2024-12-18 16:21:04

我终于让这个工作了。给定一个包含

    导航和用于加载内容的

    ,这是我的代码:

我的容器的 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

就这样了。

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:

<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 partial:

<% 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 controller:

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

That did it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文