What is NoMethodError in rails?
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
在find_item
您正在设置@post
,而不是@item
,这使得视图无效。
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
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
, ie 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)
上一篇: 我不知道为什么我的字符串无法保存值