Ruby on rails Rails新手在获取评论者姓名时遇到问题

Ruby on rails Rails新手在获取评论者姓名时遇到问题,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我的第一个项目是在rails中创建一个博客。出于某种原因,我在评论部分遇到了很多麻烦。我肯定这是我忽略的小东西 我现在可以在正确的帖子上显示评论,但我无法获得要显示的人的姓名。如果我使用comment.user.name,则会出现此错误 undefined method `name' for nil:NilClass 但是,如果我使用comment.user,它将显示如下内容:#。我在尝试执行 但是,我可以在没有错误的情况下执行comment.created_at 这里是github上的,以防

我的第一个项目是在rails中创建一个博客。出于某种原因,我在评论部分遇到了很多麻烦。我肯定这是我忽略的小东西

我现在可以在正确的帖子上显示评论,但我无法获得要显示的人的姓名。如果我使用comment.user.name,则会出现此错误

undefined method `name' for nil:NilClass
但是,如果我使用comment.user,它将显示如下内容:
#。
我在尝试执行

但是,我可以在没有错误的情况下执行comment.created_at

这里是github上的,以防更容易:

下面是显示在posts#视图上呈现的注释的部分


第一个发表评论!
这是注释模型和控制器(注释是注释内容,命名选择错误)

class注释
这是用户模型

class User < ActiveRecord::Base
 attr_accessible :email, :password, :name, :password_confirmation
 has_secure_password

 has_many :comments
 has_and_belongs_to_many :posts

 before_save { |user| user.email = email.downcase }
 before_save :create_remember_token

 validates_confirmation_of :password
 validates_presence_of :password, :on => :create
 validates_presence_of :email
 validates_uniqueness_of :email
 validates_presence_of :name

private

    def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
    end


end
class用户:create
验证是否存在:电子邮件
验证电子邮件的唯一性
验证是否存在:name
私有的
def创建\u记住\u标记
self.memory_token=SecureRandom.urlsafe_base64
结束
结束
这是Post模型和控制器

class Post < ActiveRecord::Base
  attr_accessible :content, :preview, :title
  has_many :comments, dependent: :destroy, :order => "created_at DESC"



  validates :content, presence: true
  validates :title, presence: true
end



class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])
    @comment = @post.comments.build

  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end
class Post
验证:内容,状态:true
验证:标题,状态:true
结束
类PostsController<应用程序控制器
#获取/发布
#GET/posts.json
def索引
@posts=Post.all
回应待办事项|格式|
format.html#index.html.erb
format.json{render json:@posts}
结束
结束
#获取/posts/1
#GET/posts/1.json
def秀
@post=post.find(参数[:id])
@comment=@post.comments.build
结束
#获取/发布/新建
#GET/posts/new.json
def新
@post=post.new
回应待办事项|格式|
format.html#new.html.erb
format.json{render json:@post}
结束
结束
#获取/发布/1/编辑
定义编辑
@post=post.find(参数[:id])
结束
#职位
#POST/posts.json
def创建
@post=post.new(参数[:post])
回应待办事项|格式|
如果@post.save
format.html{将_重定向到@post,注意:'post已成功创建。}
format.json{render json:@post,status::created,location:@post}
其他的
format.html{呈现操作:“新建”}
format.json{render json:@post.errors,status::unprocessable_entity}
结束
结束
结束
#PUT/posts/1
#PUT/posts/1.json
def更新
@post=post.find(参数[:id])
回应待办事项|格式|
如果@post.update_属性(参数[:post])
format.html{将_重定向到@post,注意:'post已成功更新。}
format.json{head:no_content}
其他的
format.html{呈现操作:“编辑”}
format.json{render json:@post.errors,status::unprocessable_entity}
结束
结束
结束
#删除/posts/1
#删除/posts/1.json
def销毁
@post=post.find(参数[:id])
@事后销毁
回应待办事项|格式|
format.html{redirect_to posts_url}
format.json{head:no_content}
结束
结束
结束

抱歉发了这么长的帖子!提前谢谢你的帮助

@comment=@post.comments.build
在你的帖子模型中的
show
方法正在你的
comments
集合中创建一条新的评论,但是该新评论没有用户名。

你确定没有没有没有用户的评论吗?您可以始终使用
comment.user。请尝试(:name)
以避免在这种情况下引发错误。所有注释都应该有一个用户。除非您已登录,否则无法留下评论。如果comment.user有效,而comment.user.name无效,并且是一致的,那么炖煮是非常奇怪的。很奇怪,我觉得很难相信。我把它推到github上,这样你就可以自己试试了:你能扩展一下吗?我不是想得到一个评论名,我是想得到一个用户名。
class Comment < ActiveRecord::Base
attr_accessible :comment, :post_id
belongs_to :post
belongs_to :user

validates :comment, presence: true

end


class CommentsController < ApplicationController

      def create
        @comment = current_user.comments.build(params[:comment])
        if @comment.save
            redirect_to @comment.post
            else
                render '/blog'
            end
    end

def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy
end

end
class User < ActiveRecord::Base
 attr_accessible :email, :password, :name, :password_confirmation
 has_secure_password

 has_many :comments
 has_and_belongs_to_many :posts

 before_save { |user| user.email = email.downcase }
 before_save :create_remember_token

 validates_confirmation_of :password
 validates_presence_of :password, :on => :create
 validates_presence_of :email
 validates_uniqueness_of :email
 validates_presence_of :name

private

    def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
    end


end
class Post < ActiveRecord::Base
  attr_accessible :content, :preview, :title
  has_many :comments, dependent: :destroy, :order => "created_at DESC"



  validates :content, presence: true
  validates :title, presence: true
end



class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])
    @comment = @post.comments.build

  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end