Html 在提要中的关联消息旁边显示用户

Html 在提要中的关联消息旁边显示用户,html,ruby-on-rails,ruby,Html,Ruby On Rails,Ruby,尽管我遇到了麻烦,但回答起来应该很简单: 我有一个简单的rails应用程序,其中有一个message('intro')选项卡,显示发送和接收的消息('intro')。我有从用户到用户的消息路由,并且消息的内容在用户收件箱中显示良好。但是,我在显示与消息关联的用户名称时遇到了问题,这些用户的名称位于消息本身的旁边 我有一个用户模型: class User < ActiveRecord::Base attr_accessible :name, :email, :one_liner, :

尽管我遇到了麻烦,但回答起来应该很简单:

我有一个简单的rails应用程序,其中有一个message('intro')选项卡,显示发送和接收的消息('intro')。我有从用户到用户的消息路由,并且消息的内容在用户收件箱中显示良好。但是,我在显示与消息关联的用户名称时遇到了问题,这些用户的名称位于消息本身的旁边

我有一个用户模型:

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用户
,介绍(信息)模型:

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

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
  ...
class UsersController
my.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>

收到介绍()
发送介绍()
所以我关心的是这一页的内容和行数

当前,它显示以下内容(不带关联用户的简介内容):


如何将这些用户名添加到各自的介绍中?谢谢

看起来您正在根据控制器操作中与用户相同的id查找简介。因为它是第二次查找的,所以它覆盖了@user变量。这是您的代码:

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

我猜您可能希望第二行类似于params[:intro_id],但如果没有看到链接到该页面的视图代码以及可能的路由文件,则无法完全确定。

没有做到这一点。请记住,用户模型包含许多/贯穿行项目。我是否缺少一个@x=User.find(params[:y][:z])?不,我要指出的问题是,您正在查找一个具有相同id的用户和一个简介。此外,不清楚控制器中的“@Intro”是什么。如果你去掉“@intro=…”行,会有帮助吗?好的,谢谢!不,仅仅去掉@intro行是没有帮助的。我想我肯定需要向用户控制器(show字段)添加代码,以提取与每个介绍相关联的发送者和接收者。在“@sent_intros”行中的“current_user.sent_intros.paginate(page:params[:page])”之前的内容?
@user = User.find(params[:id])
@intro = Intro.find(params[:id])