2013-07-29 2 views
0

게시물, 사용자 (Devise의 인증) 및 댓글이있는 블로그를 만들고 있습니다. 사용자가 게시글에 댓글을 쓰면 그의 이름 위에 자신의 이름을 표시하고 싶습니다. 어떻게해야합니까?게시 할 댓글 (레일스)을 작성한 사용자의 이름을 표시하는 방법

class Comment < ActiveRecord::Base 
attr_accessible :post_id, :text 
belongs_to :post 
belongs_to :user 
end 

class User < ActiveRecord::Base 
has_many :posts, :dependent => :destroy 
has_many :comments, :dependent => :destroy 

validates :fullname,  :presence => true, :uniqueness => true 
validates :password,  :presence => true 
validates :email,   :presence => true, :uniqueness => true 


devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

attr_accessible :email, :password, :password_confirmation, :fullname 
end 


class Post < ActiveRecord::Base 
attr_accessible :text, :title, :tag_list 
acts_as_taggable 

validates :user_id, :presence => true 
validates :title, :presence => true 
validates :text, :presence => true 

belongs_to :user 
has_many :comments 
end 

답변

0

그냥 연구 좀 더 많은 시간을 보내고 comments_controller.rb

def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.build(params[:comment]) 
    @comment.user = current_user 
    @comment.save 
    redirect_to @post 
end 

다음 시간에 사용자를 할당 :

class CommentsController < ApplicationController 
def create 
@post = Post.find(params[:post_id]) 
@comment = @post.comments.build(params[:comment]) 
@comment.save 
redirect_to @post 
end 

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

내 모델 : 나를

에게 내 의견 컨트롤러를 도와주세요 귀하의 질문, 이것은 일반적인 작업과 매우 간단한 Google 검색입니다 당신에게 구하기의 어려움을 덜어 주었을 것입니다.

+0

답장을 보내 주셔서 감사합니다.하지만 제가 작성한 후에는 댓글 옆에있는 사용자 이름이 여전히 비어 있습니다. 내 쇼 파일에서 나는 comment.user 메소드를 사용한다. 맞아? – user2596615

+0

아마도 <% = Comment.user.fullname %> – XanderStrike

+0

을 찾고있을 것입니다. 나는 코멘트 컨트롤러에 user_id를 추가하는 마이그레이션을 만들었습니다. 어쨌든 도움 주셔서 감사합니다. – user2596615

관련 문제