私はこれらの2つのモデルuserを持っています:
class User < ActiveRecord::Base
attr_accessible :name, :provider, :uid
# This is a class method, callable from SessionsController
# hence the "User."
def User.create_with_omniauth( auth)
user = User.new()
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.save
return user
end
has_one :userprofile
end
およびユーザープロファイル:
class Userprofile < ActiveRecord::Base
belongs_to :user
attr_accessible :age, :fname, :gender, :lname, :photo_url
end
ユーザーに関連付けられた userprofile オブジェクトがあるかどうかを確認したいと思います。ある場合は、それを表示します。それ以外の場合は、新しいものを作成してください。
私はこれを試しており、エラーが発生しています。
def show
@userprofile = current_user.userprofiles.all.where(:user_id => current_user.uid)
if !@userprofile.nil? then
@userprofile
else
@userprofile = Userprofile.new
end
end
未定義のメソッド「userprofiles」 #
findを試しましたが、より良い結果はありませんでした。