I have model User. And one of controller methods is
def view_messages
@user = User.find(params[:id])
@message=Message.new
@messages=@user.messages.reverse
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
General issues:
user_fromshould beuser_from_id, by Rails conventionThen you should have a belongs_to relation:
which will automatically pull the user from the database
Then you can refer to the user as an attribute of Message
Check all of the Message records. One of them probably has no
user_from, and causes the error.