[RUBY] I want to display the name of the poster of the comment

I want to display the name of the poster of the comment in the comment list when the three relations of User, Post, and Comment are as follows.

user.rb


has_many :posts
has_many :comments

post.rb


belongs_to :user
fas_many :comments

comment.rb


belongs_to :user
belongs_to :post

ruby:show.html.erb


<p>List of comments</p>
<% @comments.each do |c| %>
    <hr>
    <a href="/users/#{c.user_id}"><%= c.user.name %></a>
    <%= c.content %>
<% end %>

Since the name of c.user.name becomes NoMethodError, the user information related to comment can be incorporated by adding the following line to posts.controller.

ruby:posts.controller.rb


def show
   @comments = @post.comments.includes(:user).all
end

Recommended Posts

I want to display the name of the poster of the comment
I want to narrow down the display of docker ps
I want to output the day of the week
I want to display the number of orders for today using datetime.
I want to get the field name of the [Java] field. (Old tale tone)
I want you to use Enum # name () for the Key of SharedPreference
I want to know the answer of the rock-paper-scissors app
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to add a delete function to the comment function
I want to change the log output settings of UtilLoggingJdbcLogger
I want to give a class name to the select attribute
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
[Rails] I want to display the link destination of link_to in a separate tab
I want to give edit and delete permissions only to the poster
I want to control the display of the upper management navigation bar (Control menu) in Liferay 7 / DXP
I want to understand the flow of Spring processing request parameters
I want to display background-ground-image on heroku.
The story of Collectors.groupingBy that I want to keep for posterity
I want to limit the input by narrowing the range of numbers
I want to display the images under assets/images in the production environment
I want to control the default error message of Spring Boot
I want to change the value of Attribute in Selenium of Ruby
I want to know the JSP of the open portlet when developing Liferay
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
[Ruby] I want to display posted items in order of newest date
I want to display an error message when registering in the database
[Ruby] Code to display the day of the week
How to display the result of form input
I want to truncate after the decimal point
I want to get the value in Ruby
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
Development memo ~ I want to display only the first image posted multiple times ~
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
I want to separate the handling of call results according to the API caller (call trigger)
I want to see the contents of Request without saying four or five
I want to recursively get the superclass and interface of a certain class
I want to call a method of another class
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I want to judge the range using the monthly degree
I tried to reduce the capacity of Spring Boot
I summarized the display format of the JSON response of Rails
I want to dark mode with the SWT app
I want to get a list of only unique character strings by excluding fixed character strings from the file name
[Rails] How to change the column name of the table
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to simplify the log output on Android
[Ruby] I want to make a program that displays today's day of the week!
Rails The concept of view componentization of Rails that I want to convey to those who want to quit
[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
I want to convert characters ...