对象存在时nil:NilClass的未定义方法“名称”

时间:2018-11-23 13:11:46

标签: ruby-on-rails

我正在使用ruby on rails创建一个博客,并且在编写评论视图时遇到了奇怪的错误,我找不到解决方案

我正试图从comment.user中读取用户名,并返回此错误信息

undefined method `name' for nil:NilClass

如果我确实检查了用户对象,我会看到里面的数据

<%= comment.user.inspect %>

我正确添加了模型之间的关联

用户模型

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


  has_many :comments, dependent: :destroy


end

帖子模型

class Post < ApplicationRecord
    belongs_to :user
    has_many :comments, dependent: :destroy
end

评论模型

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :post
end

评论视图

<strong><%= comment.user.name %>:</strong><%= comment.body %><br><br>

请帮忙吗?

编辑2:

注释控制器代码

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @post = Post.find params[:post_id]
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)
    @post = Post.find params[:post_id]
    @comment.user_id = current_user.id 
    respond_to do |format|
      if @comment.save
        format.html { redirect_to @post, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { render :show, status: :ok, location: @comment }
      else
        format.html { render :edit }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:body, :user_id, :post_id)
    end
end

3 个答案:

答案 0 :(得分:1)

可以尝试吗?

<%= comment.user.name if comment.user %>

答案 1 :(得分:1)

尝试这个<%= comment.user.try(:name) %>

某些注释可能不包含用户价值。

答案 2 :(得分:0)

try方法是在Rails的Object类和NilClass类上定义的,它不是Ruby本身的一部分。在处理潜在的零对象时,可以使用try

<%= comment.user.try(:name) %>.

在这种情况下使用try时,不会引发NoMethodError异常,并且将返回nil,而不是接收对象是nil对象或NilClass。

有关更多信息,您可以查找此link