I am not a newbie in rails so this is embarassing but I keep getting a nomethoderror I don't know what s happening.
this is the error
and this is my controller
I am not a newbie in rails so this is embarassing but I keep getting a nomethoderror I don't know what s happening.
this is the error
and this is my controller
This happens when there's no matching method in the instance that you're accesing in this case, in this case if you look at the show
action it only does the find_item
and the find_item
only returns a @post
instance not an @item
instance.
Just change @post
to @item
in the controller
In find_item
you are setting @post
, not @item
, which makes it nil on the view.
ActiveRecord automatically gives you getter and setter methods for the instance attributes. @item.title
calls title
method on the @item
object. If the method is not defined on the object on which it is being called then NoMethodError
exception is raised.
The error that you see when you access show is because @item instance variable is not initialized in the controller and thus not available in the view file show.html.erb
, i.e. it is nil by default and Rails is complaining that there is no method title on the nil
object(as nil object doesnt have that method, @item
or @post
might have it depending on whether the respective table has that column)