Ruby on Rails Sunday, September 5, 2010

On Sep 5, 10:53 pm, nobosh <bhellm...@gmail.com> wrote:
> Ed thanks for the reply. I'd love to hear more... I tried your
> suggestion but it error'd:
>
> "undefined method `Notes' for nil:NilClass"
>

Two possible issues:

1. Do you have the relationship declared in the models?

class Note < ActiveRecord
belongs_to :user
end

class User < ActiveRecord
has_many :notes
end

2. If @user is a nil object, it will throw an error. Set @user to
the current_user in your before_filter. If there is a possibility of
hitting that point with a nil user, then change the line to read
something like this:

@note = @user ? @user.notes.find(params[:id]) : nil

which is a shorter way of saying

if @user.nil?
@note = nil
else
@note = @user.notes.find(params[:id])
end

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment