0

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

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

undefined method `name' for nil:NilClass

但是,如果我做 comment.user 它将显示如下内容:#<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

对不起,很长的帖子!我在这里先向您的帮助表示感谢!

4

1 回答 1

0

@comment = @post.comments.build在您的 Post 模型的show方法中,正在您的comments收藏中创建一个新评论,但该新评论没有用户名。

于 2013-05-12T02:53:58.220 回答