我正在尝试创建一个rails应用程序。我是新手,所以只是玩游戏。
到目前为止,我已经设置了一个设计认证系统。它创建了一个用户模型。我有一个页面模型和控制器。现在我想知道我是如何创建用户个人资料的?
另外,我如何获得用户姓名,电子邮件和内容等值?我应该生成什么?用户控制器或者如果我生成UserProfile
模型,那么当用户使用设计注册时,如何获得用户电子邮件等值。
答案 0 :(得分:0)
您可以将所有需要的字段添加到模型中,由设计生成(例如用户)。如果要将所有数据保存在单独的模型(UserProfile)中,则需要创建has_one / belongs_to关联:
class User < ActiveRecord::Base
has_one :user_profile
# ... Some other stuff here
end
class UserProfile < ActiveRecord::Base
belongs_to :user
# ...
end
要获取UserProfile的电子邮件,您可以:
profile = UserProfile.first
email = profile.user.email
从User对象获取UserProfile:
user = User.first
profile = user.user_profile
为了能够存储有关页面作者的信息,您应该使用has_many / belongs_to association:
class User < ActiveRecord::Base
has_many :pages
# ... Some other stuff here
end
class Page < ActiveRecord::Base
belongs_to :user
# ... Some other stuff here
end
在Page model的某些视图中,您可以显示以下作者:
Page author is <%= @page.user.user_profile.name %>
或者,如果您决定将字段直接添加到用户模型
Page author is <%= @page.user.name %>