2014-02-05 6 views
0

사용자가 게시물에 댓글을 달 수있는 응용 프로그램이 있습니다. http://guides.rubyonrails.org/association_basics.html 다음 commenter가 당신은 협회에 레일 가이드를 확인해야 주석게시물 덧글을 사용자와 연결

+0

그것은 당신이 정확한 수 약간? 댓글 모델을 작성하여 댓글을 작성한 사용자에게 반환 하시겠습니까? 그렇다면이 열을 문자열로 나타내시겠습니까? 아니면 사용자 ID로 확인 하시겠습니까? – BroiSatse

+0

내가 충분히 설명하지 못해서 미안해. 예, 댓글을 작성한 특정 사용자를 댓글 모델로 반환하겠습니다. 내가 문자열 (즉 ... 사용자가 "주석기"이름을 입력 할 수있는 순간에)가되고 싶지는 않습니다. 사용자가 본문에 텍스트를 입력 할 수있게하려는 경우에만 해당 user_id가 해당 텍스트와 연결됩니다. 논평. – Aluxzi

답변

0

를 게시 한 사용자로 설정되도록

rails generate model Comment commenter:string body:text post:references

내가 그것을 어떻게 변경할 수 있습니다.

아마도 사용자 모델은 코멘트 모델과 has_many belongs_to 관계를 유지해야합니다.

댓글 리소스에는 정수 : user_id 열이 있어야합니다. 앱에서 @ comment.user를 호출하여이 정보에 액세스 할 수 있습니다.

1

당신은 할 필요 협회 : 나는 마침내 문제를 해결하는 방법을 발견

#Comment controller? Hard to say how it is being saved from the code you posted. :P 

    def create 
    @comment = Comment.new(params[:comment]) 
    @comment.user = current_user 
    if @comment.save 
     ... 
    end 
    end 
1

: 새 주석을 만들 때 당신은이 값을 할당해야합니다

rails generate model Comment author_id:integer body:text post:references 

class Comment < ActiveRecord::Base 

    belongs_to :post 
    belongs_to :author, class_name: <User or whatever your user model is called>, foreign_key: :author_id 

end 

class User < ActiveRecord::Base 
    has_many :comments, foreign_key: :author_id 
end 

.

rails generate model Comment user_id:integer body:text listing:references

후 코멘트 양식에 숨겨진 속성을 추가 \app\models\comments.rb 파일에 attr_accessible:user_id을 추가

<%= f.hidden_field :user_id, :value => current_user.id %>

을 다음 사용 <%= comment.user.id %> 및/또는 <%= comment.user.name %>

관련 문제