Rails 3 link_to 部分带有 for 循环 [英] Rails 3 link_to partial with for loop inside

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

问题描述

我以前从未编程过,并且很难让 link_to 在我的 Rails 3 应用程序内的用户配置文件中呈现相应的 :partial.一共分为三个部分:

  1. _profile_credits(为了这个问题,我专注于 _profile_credits.)
  2. _profile_about
  3. _profile_reviews

我想要做的是点击以下内容:

  • <%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote =>真%>/li>
  • 并在@user.credits 中加载和呈现以下部分(_profile_credits):

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

    <%结束%>

    我尝试渲染部分的位置在链接下方和 div 容器内部.下面的 HTML 显示了 div 中链接的位置以及我希望加载部分信息的位置:

    <ul id="infoContainer"><li><%= link_to "Reviews", profile_reviews_profile_path(:id => @profile.id), :remote =>真%>/li><li><%= link_to "About", profile_about_profile_path(:id => @profile.id), :remote =>真%>/li><li><%= link_to "Credits", profile_credits_profile_path(:id => @profile.id), :remote =>真%>/li><div id="tabs-1">##从`<div id="tabs-1">内的partials加载信息

    </div><!-- 结束标签-->

    我的其余代码...

    我的profiles_controller#profile_credits操作:

    def profile_creditsresponse_to do |格式|format.js { 渲染:布局 =>错误的 }结尾结尾

    在我的 routes.rb 中:

    resources :profiles 做获取 :profile_credits, :on =>:成员结尾

    我的profile_credits.js.erb:

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

    目前当我点击一个链接时没有任何反应.我试过遵循各种 Rails 3 UJS 示例,但无法理解.有一次,我在 ProfilesController profile_credits 操作中指定了更多内容.但是,如果我在其他人的个人资料上加载我的积分,而不是我正在浏览其个人资料的用户的积分.谁能帮我解决这个问题?

    解决方案

    我终于让这个工作了.给定一个

    包含一个
      导航和一个

      用于加载的内容,这是我的代码:

      我的容器的 HTML:

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

    </div><!-- 结束标签-->

    _profile_credits.html.erb 部分:

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

    <%结束%>

    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).creditsresponse_to do |格式|format.html { 渲染:布局 =>零 }格式.xmlformat.js { 渲染:布局 =>零 }结尾结尾

    做到了.

    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?

    解决方案

    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.

    这篇关于Rails 3 link_to 部分带有 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    前端开发最新文章
    热门教程
    热门工具
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆