繁体   English   中英

如何访问关联模型的关联模型?

[英]How do I access an associated model of an associated model?

我有一个主题,有很多帖子。 每个帖子属于一个用户,每个用户都有一个个人档案。

在我针对特定主题的“显示”页面中,我尝试显示创建帖子的用户的个人资料信息:

<% @topic.posts.each do |post| %>

<%= post.user.profile.first_name %>

<% end %>

我收到以下错误:

nil的未定义方法`profile':NilClass

知道为什么它不允许我访问个人资料吗? 请指教。

我的主题控制器如下:

class TopicsController < ApplicationController
  # GET /topics
  # GET /topics.json
  add_breadcrumb :index, :topics_path

  def index
    if params[:tag]
        @topics = Topic.tagged_with(params[:tag])
    else
        @topics = Topic.all
    end

    @newtopic = Topic.new


    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @topics }
    end
  end

  # GET /topics/1
  # GET /topics/1.json
  def show
    @topic = Topic.find(params[:id])
    @posts = @topic.posts
    @newpost = @topic.posts.build
    add_breadcrumb @topic.name

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @topic }
    end
  end

  # GET /topics/new
  # GET /topics/new.json
  def new
    add_breadcrumb :new, :topics_path
    @topic = Topic.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @topic }
    end
  end

  # GET /topics/1/edit
  def edit
    @topic = Topic.find(params[:id])
  end

  # POST /topics
  # POST /topics.json
  def create
    @topic = Topic.new(params[:topic])

    @topic.user_id = current_user.id
    @topic.last_poster_id = current_user.id
    @topic.last_post_at = Time.now


    respond_to do |format|
      if @topic.save
        format.html { redirect_to @topic, notice: 'Topic was successfully created.' }
        format.json { render json: @topic, status: :created, location: @topic }
      else
        format.html { render action: "new" }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /topics/1
  # PUT /topics/1.json
  def update
    @topic = Topic.find(params[:id])

    respond_to do |format|
      if @topic.update_attributes(params[:topic])
        format.html { redirect_to @topic, notice: 'Topic was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @topic.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /topics/1
  # DELETE /topics/1.json
  def destroy
    @topic = Topic.find(params[:id])
    @topic.destroy

    respond_to do |format|
      format.html { redirect_to topics_url }
      format.json { head :no_content }
    end
  end
end

检查您的数据库。 很可能在您的数据库中有一个对应于没有用户的帖子。 由于该帖子的用户为none,因此nil:NilClass的配置文件未定义nil:NilClass是user(null)。

这种情况主要发生在您创建属于用户的帖子但随后从数据库中删除属于该帖子的用户时。

正确的方法是在您的用户模型中强加一个约束,该约束应该是 -

class Post
 belongs_to :user, :dependent => :destroy
end

因此,如果用户被删除,该用户的相应帖子也会被删除。

请注意,在使用表格强制使用表格之间的关系后直接从数据库中删除记录不是一个好习惯。

您的错误是由show action @topic.posts.build这一行以及view @topic.posts.each中的这一行@topic.posts.each 由于您正在控制器中构建新帖子,因此@topic.posts包含最可能将用户设置为nil的新记录。 因此,您的问题的解决方案是在视图中使用@posts而不是@topic.posts

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM