簡體   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