Rails新手无法获得评论者姓名显示

时间:2013-05-12 02:37:57

标签: ruby-on-rails ruby-on-rails-3

我正在使用rails构建一个博客作为我的第一个项目。出于某种原因,我在评论部分遇到了很多麻烦。我敢肯定这是我想要的小事。

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

undefined method `name' for nil:NilClass

但是,如果我发表评论,则会显示如下内容:#<User:0x466cd28>.我在尝试<%=time_ago_in_words(comment.created_at) %>

时也会收到错误
undefined method `year' for nil:NilClass

但是我可以做没有错误的comment.created_at。

这是github,以防更容易:https://github.com/Mciocca/blog

以下是显示帖子#view

上呈现的评论的部分
 <% if @post.comments.any? %>
 <% @post.comments.each do |comment| %>
 <div class="comment-name">
 <%= comment.user %>  <%=comment.created_at%></div> 
 <div class='comment-content'>
<%= comment.comment %>
</div> 
<% end %>
<% else %>
<h3>Be the first to comment!</h3>
<% end %>

这是评论模型和控制器(评论是评论内容,错误的命名选择)

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

这是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

对不起,很长的帖子!感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Post模型的@comment = @post.comments.build方法中的

show正在comments集合中创建新评论,但该评论没有用户名。