Ruby on rails 如何访问关联模型的关联模型?
我有一个主题有很多帖子。每个帖子都属于一个用户,每个用户都有一个配置文件 在特定主题的“显示”页面中,我尝试显示创建帖子的用户的个人资料信息:Ruby on rails 如何访问关联模型的关联模型?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有一个主题有很多帖子。每个帖子都属于一个用户,每个用户都有一个配置文件 在特定主题的“显示”页面中,我尝试显示创建帖子的用户的个人资料信息: <% @topic.posts.each do |post| %> <%= post.user.profile.first_name %> <% end %> 我得到以下错误: nil:NilClass的未定义方法“profile” 知道为什么它不允许我访问个人资料吗?请告知 我的主题控制器如下: class
<% @topic.posts.each do |post| %>
<%= post.user.profile.first_name %>
<% end %>
我得到以下错误:
nil:NilClass的未定义方法“profile”
知道为什么它不允许我访问个人资料吗?请告知
我的主题控制器如下:
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
class TopicsController
检查您的数据库。很可能在您的数据库中有一篇文章没有对应任何用户。由于该帖子的用户是none,因此nil:NilClass
的用户(null)的配置文件变得未定义
这种情况通常发生在创建属于用户的帖子时,然后从数据库中删除属于该帖子的用户
正确的方法是在用户模型中施加一个应该-
class Post
belongs_to :user, :dependent => :destroy
end
因此,如果用户被删除,该用户的相应帖子也会被删除
请注意,在使用表强制记录之间的关系后直接从数据库中删除记录不是一种好的做法。您的错误是由show action
@topic.posts.build
中的这一行以及视图@topic.posts.each
中的这一行引起的。由于您正在控制器中构建一个新的post,@topic.posts
包含该新记录,该记录很可能将用户设置为nil。因此,您的问题的解决方案是使用@posts
而不是在您的视图中使用@topic.posts
。您有一篇用户为零的帖子,这会导致错误。感谢您的回复。我查过了,没有没有没有用户id的帖子。还有什么问题吗?谢谢你的评论。我更新了关系,以便有:dependent=>:destroy。但我也检查了所有的记录,没有一篇帖子没有有效的用户id。关于它可能是什么,还有其他想法吗?谢谢