我收到以下错误:
undefined method `=' for #<CabinetItem:0x490e6f0>
我的代码都没有使用等号。你能看到问题吗?感谢。
查看电话:
<%= link_to "Toggle", {:controller => :cabinet_items, :action => :toggle_backmount, :id => cabinet_item.id } %>
controlloer方法:
def toggle_backmount
@cabinet_item = CabinetItem.find(params[:id])
@cabinet_item.toggle_backmount
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
模型方法:
def toggle_backmount
if self.is_backmount
self.update_attribute(is_backmount, false)
else
self.update_attribute(is_backmount, true)
end
end
完整Stacktrace:
#<Class:0x5f0ea28>: undefined method `=' for #<CabinetItem:0x7becc28>
from C:/SOFTWARE/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.13/lib
/active_model/attribute_methods.rb:407:in `method_missing'
from C:/SOFTWARE/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/li
b/active_record/attribute_methods.rb:149:in `method_missing'
from C:/SOFTWARE/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/li
b/active_record/persistence.rb:180:in `update_attribute'
from C:/Users/n0222072/AptanaWorkspace/dcms/app/models/cabinet_item.rb:4
8:in `toggle_backmount'
from (irb):3
from C:/SOFTWARE/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/ra
ils/commands/console.rb:47:in `start'
from C:/SOFTWARE/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/ra
ils/commands/console.rb:8:in `start'
from C:/SOFTWARE/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/ra
ils/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
答案 0 :(得分:2)
update_attribute
的第一个参数应该是字符串或符号。实际上,您正在调用is_backmount
并将结果作为第一个参数传递给update_attribute
。显然在这种情况下,is_backmount
返回零。你想要:
def toggle_backmount
if self.is_backmount
self.update_attribute(:is_backmount, false)
else
self.update_attribute(:is_backmount, true)
end
end