2013-02-11 123 views
0

我有一個主題has_many帖子。每個帖子屬於一個用戶,並且每個用戶擁有一個配置文件。如何訪問關聯模型的關聯模型?

在特定主題我「秀」頁面中,我嘗試顯示誰創造了貼的用戶的配置文件信息:

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

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

<% end %> 

我收到以下錯誤:

undefined method `profile' for nil: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 
+2

你有一個帖子,其中用戶爲零,導致錯誤。 – jvnill 2013-02-11 07:24:45

+0

感謝您的回覆。我檢查了,沒有user_id沒有帖子。任何其他的想法可能是錯誤的? – 2013-02-11 21:50:45

回答

1

您的錯誤是由顯示操作@topic.posts.build中的此行和視圖@topic.posts.each中的此行所引起的。由於您正在控制器中創建一個新帖子,因此@topic.posts包含了最有可能讓用戶爲零的新記錄。所以解決您的問題的方法是在您的視圖中使用@posts而不是@topic.posts

+0

謝謝,解決了問題! – 2013-02-13 06:57:20

1

檢查數據庫。它很有可能在你的數據庫中有一個對應於沒有用戶的帖子。由於該帖子的用戶爲空,因此用戶(空)nil:NilClass的配置文件變得未定義。

這種情況主要發生在創建屬於用戶的帖子後,但您從數據庫中刪除屬於該帖子的用戶。

正確的方法是強加在你的用戶模型的約束應該是─

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

因此,如果用戶被刪除,該用戶相應的職位也被刪除。

請注意,在使用表格強加關係之後,直接從數據庫中刪除記錄並不是一種好的做法。

+0

感謝您的評論。我更新了關係,以便存在:dependent =>:destroy。但我也檢查了所有的記錄,沒有沒有有效用戶ID的帖子。任何其他想法可能是什麼?謝謝 – 2013-02-11 21:51:47