Rails注释和用户名不会显示在帖子视图/未定义方法`user'中为nil:NilClass

时间:2017-10-02 20:34:12

标签: ruby-on-rails

所以我有一个post.rb模型,在那里我可以显示谁在show post视图中写了帖子,并且效果非常好。

但是,我一直在尝试添加属于帖子的评论,也属于用户。

我似乎无法获得所有发布评论的用户的评论或名称,以显示在show.html.erb中。

我想我的问题是,如何在控制器和模型视图之间集成控制器和模型信息?我知道它是否存在于模型和控制器以及相同类型的视图中,它是可访问的,但交叉链接或共享控制器模型信息对我来说很难。

我希望能够在帖子的节目视图中显示发表评论的用户以及评论的正文,而不是评论。

comment.rb

class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user
  validates :body, :presence => true
end

post.rb

class Post < ApplicationRecord
  belongs_to :user, optional: true
  validates :user, presence: true;

  validates :title, presence: true,
                    length: { minimum: 5}


  extend FriendlyId
  friendly_id :title, use: :slugged
  validates :title, :slug, presence: true

  # comments
  has_many :comments

end

user.rb

class User < ApplicationRecord
  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :school_name, presence: true, inclusion: { in: %w(Harvard Yale),
    message: "%{value} is not a valid choice" }

  validates :graduation_year, presence: true, inclusion: { in: %w(2017 2018 2019 2020 2021 2022 2023),
    message: "%{value} is not a valid choice currently" }

    def superadmin?
      self.role.name == "Superadmin"
    end 

end

comments_controller.rb

class CommentsController < ApplicationController
before_action :authenticate_user!
# GET /posts/:post_id/comments
# GET /posts/:post_id/comments.xml

def index
#1st you retrieve the post thanks to params[:post_id]
post = Post.friendly.find(params[:post_id])
#2nd you get all the comments of this post
@comments = post.comments

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @comments }
end
 end

 # GET /posts/:post_id/comments/:id
 # GET /comments/:id.xml
 def show
 #1st you retrieve the post thanks to params[:post_id]
 post = Post.friendly.find(params[:post_id])
 #2nd you retrieve the comment thanks to params[:id]
 @comment = post.comments.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @comment }
end
end

# GET /posts/:post_id/comments/new
# GET /posts/:post_id/comments/new.xml
def new
#1st you retrieve the post thanks to params[:post_id]
post = Post.friendly.find(params[:post_id])
#2nd you build a new one
@comment = post.comments.new(params[:comment])
@comment.user = current_user

end

# GET /posts/:post_id/comments/:id/edit
def edit
#1st you retrieve the post thanks to params[:post_id]
post = Post.friendly.find(params[:post_id])
#2nd you retrieve the comment thanks to params[:id]
@comment = post.comments.find(comment_params[:id])
end

# POST /posts/:post_id/comments
# POST /posts/:post_id/comments.xml
def create
#1st you retrieve the post thanks to params[:post_id]
post = Post.friendly.find(params[:post_id])
#2nd you create the comment with arguments in params[:comment]
@comment = post.comments.create(comment_params)
@comment.user = current_user


respond_to do |format|
  if @comment.save
    #1st argument of redirect_to is an array, in order to build the correct route to the nested resource comment
    format.html { redirect_to([@comment.post, @comment], :notice => 'Comment was successfully created.') }
    #the key :location is associated to an array in order to build the correct route to the nested resource comment
    format.xml  { render :xml => @comment, :status => :created, :location => [@comment.post, @comment] }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @comment.errors, :status => :unprocessable_entity }
  end
end
end

1 个答案:

答案 0 :(得分:1)

show的{​​{1}}方法中,您需要执行以下操作:

post_controller

然后,在def show @post = Post.find_by(id: params[:id]) # or however you find your post .... end (如果您正在使用erb)中,您将执行以下操作:

views/posts/_show.html.erb