2013-05-12 29 views
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

這裏是顯示在帖子#查看

<% 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 

這裏是郵政模型和控制器

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 

對不起,長的帖子!我在這裏先向您的幫助表示感謝!

+0

您確定沒有沒有用戶的評論嗎?你總是可以使用'comment.user.try(:name)'來避免在這種情況下引發錯誤。 – MrTheWalrus 2013-05-12 02:51:39

+0

所有評論應該有一個用戶。除非您已登錄,否則不能發表評論。 – Michael 2013-05-12 02:52:35

+0

如果comment.user有效,並且comment.user.name不可用,並且它是一致的,則煨是非常奇怪的。很奇怪,我覺得很難相信。 – 2013-05-12 02:54:06

回答

0

@comment = @post.comments.build在您的Post模型的show方法正在您的comments集合中創建新評論,但該新評論沒有用戶名。

+0

你可以擴展它嗎?我不想獲得評論名稱,我試圖獲取用戶名。 – Michael 2013-05-12 04:09:10

相關問題