在Feed中的相关消息旁边显示用户

时间:2012-07-04 15:58:07

标签: html ruby-on-rails ruby

尽管我遇到了麻烦,但应该很容易回答的快速问题:

我有一个简单的rails应用程序,其中有一条消息('intro')选项卡显示已发送和已接收的消息('intros')。我有适当的消息从用户到用户,并且消息的内容在用户收件箱中显示正常。但是,我无法显示与消息本身旁边的消息相关联的用户名称

我有一个用户模型:

class User < ActiveRecord::Base
    attr_accessible :name, :email, :one_liner, :password, :password_confirmation
    has_secure_password

    has_many :sent_intros, foreign_key: "sender_id", dependent: :destroy, class_name: "Intro"
    has_many :received_intros, foreign_key: "receiver_id", dependent: :destroy, class_name: "Intro"

    has_many :receivers, through: :sent_intros, source: :receiver
    has_many :senders, through: :received_intros, source: :sender

        ...

,一个简介(消息)模型:

class Intro < ActiveRecord::Base
  attr_accessible :content, :receiver_id, :sender_id

  belongs_to :sender, class_name: "User"
  belongs_to :receiver, class_name: "User"

  ...

以下是用户控制器的相关代码:

class UsersController < ApplicationController
  before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
  before_filter :correct_user, only: [:edit, :update]
  before_filter :admin_user, only: :destroy

  def show
    @user = User.find(params[:id])
    @intro = Intro.find(params[:id])

    @sent_intros = current_user.sent_intros.paginate(page: params[:page])
    @received_intros = current_user.received_intros.paginate(page: params[:page])
  end
  ...

我的.erb显示页面:

<% provide(:title, @user.name) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>

                <%= @user.name %>
            </h1>
        </section>
    </aside>
    <div class="span8">
        <% if@user.received_intros.any? %>
            <h3>Received intros (<%= @user.received_intros.count %>)</h3>
            <ol class="intros">
                <%= render @received_intros %>
            </ol>
            <%= will_paginate @received_intros %>
        <% end %>

        <% if@user.sent_intros.any? %>
            <h3>Sent intros (<%= @user.sent_intros.count %>)</h3>
            <ol class="intros">
                <%= render @sent_intros %>
            </ol>
            <%= will_paginate @sent_intros %>
        <% end %>
    </div>
</div>

所以我关注&lt;%= render @received_intros%&gt;和&lt;%=渲染@sent_intros%&gt;本页行

目前,它显示以下内容(没有相关用户的介绍内容):

enter image description here

如何将这些用户名添加到各自的前缀?谢谢!

1 个答案:

答案 0 :(得分:0)

看起来你正在查找基于与控制器操作中的用户相同ID的Intro。因为它被查询第二,它覆盖了@user变量。这是你的代码:

@user = User.find(params[:id])
@intro = Intro.find(params[:id])

我猜你可能希望第二行像params [:intro_id],但不能完全确定没有看到链接到该页面的视图代码以及可能的路径文件。