So, i make this atribbuition in model comment.rb
has_one :user
And my form is this
<% @post.comments.each do |com| %>
Comentário<br><%= com.comment %>
<% end %>
My question is, how do I get the user when make the comment ?
I have a problem, i make this atribbuition i comment model:
class Comment < ActiveRecord::Base
attr_accessible :comment
belongs_to :post
belongs_to :user
and this in user model
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
has_many :posts
has_many :comments
but this dont works:
<% post.comments.each do |comment| %>
<div id="comments" >
<%= comment.user.email %>
<%= comment.comment %>
</div>
<%end%>
appear the error:
undefined method `email' for nil:NilClass
please what is the problem, in the create of the comment i make the atribbuition so , look:
@comment = @post.comments.create(params[:comment],:user_id => current_user.id)
how i solve this error, please
You just have to add this code
<%= com.user.username %>for example to display the user’s username