Reputation: 4410
I have model User. And one of controller methods is
def view_messages
@user = User.find(params[:id])
@message=Message.new
@[email protected]
end
Also i have model Message , and one of parametres of this model is user_from:integer
view_messages view have
render :partial => 'messages/message', :collection => @messages
and _message.haml have
= content_tag_for(:li,message) do
%p
From:
=link_to User.find(message.user_from).name,User.find(message.user_from)
it writes an error
Couldn't find User without an ID
but if i want to print it like
= content_tag_for(:li,message) do
%p
From:
=message.user_from
it print it ( for exaple it prints 2) , so why it cant find user with id 2 if i have this user? What i am doing wrong? Thanks in advance
Upvotes: 0
Views: 878
Reputation: 14179
General issues:
user_from
should be user_from_id
, by Rails convention
Then you should have a belongs_to relation:
class Message
belongs_to :user_from
end
which will automatically pull the user from the database
Then you can refer to the user as an attribute of Message
link_to @message.user_from.name, @message.user_from
Check all of the Message records. One of them probably has no user_from
, and causes the error.
Upvotes: 2