0
問題陳述
在我的控制,我試圖改變使用resource.update_attribute(resource.not_registered, 1)
在更新一個布爾屬性not_registered
,但我得到NoMethodError: undefined method 0= for #<User:0x007fe470de2950
。任何想法爲什麼發生這種情況?感謝您可能有的任何想法,這是我在完成此項目之前需要解決的最後一個錯誤,這將是我在RoR中的第一個錯誤!使用update_attribute並獲得NoMethodError:未定義的方法`0 =」#爲<用戶:0x007fe470de2950
我的進步
- 我看着使用
save
代替,但因爲我不需要跳過驗證。似乎也是低效的,因爲我只更新一個屬性 - 在shell,
resource
(通過制定創建)似乎是當前用戶 - 的一個實例,執行更新控制器
resource.not_registered => 0
,這是正確的之前...所以我知道resource.not_registered
是正確的語法。 - 如果我跑
resource.update_attribute
,我得到!! #<ArgumentError: wrong number of arguments (0 for 2)>
...所以我知道update_attribute
存在爲resource
- 最後的方法,我發現update_attribute here的文檔。
代碼
/app/controllers/registrations_controller.rb:
/app/models/user.rb:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :token_authenticatable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
attr_accessible :email, :password, :password_confirmation,
:remember_me, :not_registered, :pay_method, :pay_desc, :email_instructions, :current_password
def update_with_password(params={})
# code removed for brevity
end
end
謝謝,這確實解決了錯誤。 Rails doc很棒 – Adam
如果你接受/ upvote我的回答,我會很高興:) –
哎呀!只是做:) – Adam