[英]Creating a site with rails and need guidance with user profile and devise
我正在尝试创建一个Rails应用程序。
到目前为止,我已经建立了一个设计认证系统。 从而创建了一个用户模型。 我有一个页面模型和控制器。 现在我想知道如何创建用户个人资料?
另外,我如何获取用户名,电子邮件和内容之类的值? 我应该产生什么? 用户控制器,或者如果我生成UserProfile
模型,那么当用户使用devise注册时,如何获取用户电子邮件之类的值。
您可以将所有需要的字段添加到由devise(例如,用户)生成的模型中。 如果要将所有数据保留在单独的模型(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关联:
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模型的某些视图中,您可以显示作者,例如:
Page author is <%= @page.user.user_profile.name %>
或者,如果您决定将字段直接添加到用户模型
Page author is <%= @page.user.name %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.