Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show name of user who wrote comment (Rails)?
    primarykey
    data
    text
    <p>In my rails app I have users(authors),posts(articles),comments. If registered user write comment to article, I want to show his name beside his comment, if he isn't registered user I want to show "Anonymous" beside his comment. How can I do this?</p> <p>comment model:</p> <pre><code>class Comment &lt; ActiveRecord::Base attr_accessible :post_id, :text belongs_to :post belongs_to :user end </code></pre> <p>user model:</p> <pre><code>class User &lt; ActiveRecord::Base # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable has_many :posts, :dependent =&gt; :destroy has_many :comments, :dependent =&gt; :destroy validates :fullname, :presence =&gt; true, :uniqueness =&gt; true validates :password, :presence =&gt; true validates :email, :presence =&gt; true, :uniqueness =&gt; true devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :fullname end </code></pre> <p>post model:</p> <pre><code>class Post &lt; ActiveRecord::Base attr_accessible :text, :title, :tag_list acts_as_taggable validates :user_id, :presence =&gt; true validates :title, :presence =&gt; true validates :text, :presence =&gt; true belongs_to :user has_many :comments end </code></pre> <p>view file (show.html.erb)</p> <pre><code>&lt;h1&gt;&lt;%= @post.title %&gt;&lt;/h1&gt; &lt;p&gt; Created: &lt;%= @post.created_at.strftime("%Y/%m/%d")%&gt; by &lt;%= link_to @post.user.fullname, user_posts_path(@post.user) %&gt; &lt;/p&gt; &lt;p&gt;&lt;%=simple_format @post.text %&gt;&lt;/p&gt; &lt;p&gt; Tags: &lt;%= raw @post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %&gt; &lt;/p&gt; &lt;h2&gt;Comments&lt;/h2&gt; &lt;% @post.comments.each do |comment| %&gt; &lt;p&gt;&lt;%= comment.created_at.strftime("%Y/%m/%d") %&gt; by &lt;%= HERE I NEED ADD SOMETHING%&gt;&lt;/p&gt; &lt;p&gt;&lt;%= comment.text %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= link_to "Delete comment", [@post, comment], :method =&gt; :delete, :confirm =&gt;"Are you sure?"%&gt;&lt;/p&gt; &lt;% end %&gt; &lt;%= form_for [@post, @post.comments.build] do |f| %&gt; &lt;p&gt;&lt;%= f.text_area :text %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= f.submit "Post comment" %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;% if user_signed_in?%&gt; &lt;p&gt; &lt;%= link_to "Back", posts_path %&gt; &lt;%= link_to "Edit", edit_post_path(@post) %&gt; &lt;%= link_to "Delete", @post, :method =&gt; :delete, :confirm =&gt; "Are you sure?"%&gt; &lt;/p&gt; &lt;% end%&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload