[英]Rails/Devise: How can I find out in the controller if the user-login was successful or not?
[英]In Rails 4, how can I get data out of a devise user within a controller?
我有一個現成的devise User類,並且有一個方法可以獲取特定用戶並在控制台上打印其電子郵件地址
def print_email
user = User.where('id'=>params[:id])
puts user.email
end
但是,這給了我一個錯誤
NoMethodError (undefined method `email' for #<User::ActiveRecord_Relation:0x007ff288919808>):
為確保獲得合適的用戶,我添加了
puts user.to_json
這會打印出預期的json,因此正確的User將存儲在user變量中,但是我根本無法直接訪問該用戶的email屬性。
我怎樣才能做到這一點?
查找應返回不是ActiveRecord_Relation的User對象。 如果它是ActiveRecord_Relation,請使用
user.first.email
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.