Home>
..

I am making a bulletin board with RubyOnRails.
The following 5 items should be displayed on the bulletin board (message index).
① User ID
② User name (name of the person who posted the message)
③ Message title
⑤ Contents of the message

② cannot be displayed.
(Can be current_user, but
Then, of course, every message will be the name of the logged-in user.
Right now, "@user" is written in "index.html" temporarily, but it is blank on the screen.
I want to display the name of the person who posted the message)

Error message
The following Rails errors are displayed.
User must exist

Corresponding source code

RubyOnRails

Source code
"Messages_controller.rb"
class MessagesController before_action: set_message, only: [: show,: edit,: update,: destroy]
before_action: require_user_logged_in, only: [: index,: show,: new,: new ,: create,: edit,: update,: destroy]

def index
@messages = Message.order (id:: desc) .page (params [: page])
end

def show
@message = Message.find_by (id: params [: id])
end

def new
@message = Message.new
@ message.user_id = current_user.id
@ message.save
end

def create
@message = Message.new (message_params)

if @ message.save
flash [: success] ='Message posted successfully'
redirect_to @message
else else
flash.now [: danger] ='Message was not posted'
render: new
end
end

def edit
end

def update

if @ message.update (message_params)
flash [: success] ='Message updated successfully'
redirect_to @message
else else
flash.now [: danger] ='Message was not updated'
render: edit
end
end

def destroy
set_message
@ message.destroy

flash [: success] ='Message was successfully deleted'
redirect_to messages_url
end

private

def set_message
@message = Message.find (params [: id])
end

Strong Parameter

def message_params
params.require (: message) .permit (: content,: title)
end
end

"Index.html"
Message list<% = link_to'Post new message', new_message_path, class: "btn btn-primary"%>

<% if @ messages.any?%>











<% @ messages.each do | message |%>






<% end%>

idusernameTitleMessage
<% = link_to message.id, message%><% = @user%><% = message.title%><% = message.content%>


<% = paginate @messages%>

<% end%>

"Message.rb"
class Message belongs_to: user
validates: content, presence: true, length: {maximum: 255}
validates: title, presence: true, length: {maximum: 255}
end

What I tried

Where "@user" is written in "index.html", it is written as message.user.name etc.
I got an error "undefined method` name' for nil: NilClass ".

Supplementary information (FW/tool version, etc.)

The table looks like this
describe messages;
+ ------------ + -------------- + ------ + ----- + -------- -+ ---------------- +
Field | Type | Null | Key | Default | Extra |
+ ------------ + -------------- + ------ + ----- + -------- -+ ---------------- +
id | bigint (20) | NO | PRI | NULL | auto_increment |
| content | varchar (255) | YES | | NULL | |
| user_id | bigint (20) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| title | varchar (255) | YES | | NULL | |
+ ------------ + -------------- + ------ + ----- + -------- -+ ---------------- +

mysql>describe users;
+ ----------------- + -------------- + ------ + ----- + --- ------ + ---------------- +
Field | Type | Null | Key | Default | Extra |
+ ----------------- + -------------- + ------ + ----- + --- ------ + ---------------- +
id | bigint (20) | NO | PRI | NULL | auto_increment |
| name | varchar (255) | YES | | NULL | |
| email | varchar (255) | YES | | NULL | |
| password_digest | varchar (255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+ ----------------- + -------------- + ------ + ----- + --- ------ + ---------------- +

  • Answer # 1

    There is a user_id in the messages table, but is the value of user_id included for a certain message?

    Also, does the user exist in the users table for that user_id?

    If these two points are met,

    message.user.name

    This should print the user's name.