I’m running Rails 3 and like to have a message system.
Here is the tutorial for it: http://www.novawave.net/public/rails_messaging_tutorial.html
It’s for Rails 2 so I was trying to implement it in Rails 3.
But after I build the “/view/sent/index/” and try to send a message (even no one can’t read it) i got an error:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
app/models/message.rb:16:in `prepare_copies'
app/models/message.rb:14:in `each'
app/models/message.rb:14:in `prepare_copies'
app/controllers/sent_controller.rb:19:in `create'
My “sent” Controller looks like:
def create
@message = current_user.sent_messages.build(params[:message])
if @message.save
flash[:notice] = "Message sent."
redirect_to :action => "index"
else
render :action => "new"
end
end
Line #19 would be “if @message.save”
So i googled and found out that @message might be nil but i can’t get the reason why.
The “view/sent/new/” form is like:
<% form_for :message, :url => {:controller => "sent", :action => "create"} do |f| %>
<p>
To:<br />
<select name="message[to][]">
<%= options_from_collection_for_select(User.find(:all), :id, :username, @message.to) %>
</select>
</p>
<p>Subject: <%= f.text_field :subject %></p>
<p>Body:<br /> <%= f.text_area :body %></p>
<p><%= f.submit %></p>
<% end %>
On the error page the parameters are correct submitted:
Parameters:
{"commit"=>"Save Message",
"authenticity_token"=>"GtJEl2DnKHy06e3IepnnZU0u9RWNlO7gDOx/7FyXFBo=",
"utf8"=>"✓",
"message"=>{"body"=>"test",
"to"=>["2"],
"subject"=>"test"}}
I changed some lines in “routes.rb” but i think this is not the point here.
Maybe someone got a hint?
Greets
Based on the tutorial, here is the code that’s erroring:
So either recipient is nil or recipient.inbox is nil. Presumably it’s the latter because the select is populated with the existing users. A user’s inbox is created in the before hook when the user is created. Were all of your users created via the app?
Open up rails console and try User.find(2) and User.find(2).inbox. If neither of those is nil, you may have to use the debugger or put some prints in the code to figure out what is nil and why.