Ruby on rails 创建用户配置文件和用户与设备的关联
我正在尝试创建一个用户配置文件模型,用于Deviate的用户模型 我有以下资料: 用户类Ruby on rails 创建用户配置文件和用户与设备的关联,ruby-on-rails,activerecord,devise,Ruby On Rails,Activerecord,Devise,我正在尝试创建一个用户配置文件模型,用于Deviate的用户模型 我有以下资料: 用户类 class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable,
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :user_profile
accepts_nested_attributes_for :user_profile
after_create :build_profile
private
def build_profile
user_profile.build_user_profile
end
end
class用户
用户配置文件类
class UserProfile < ActiveRecord::Base
belongs_to :user
validates :username, presence: true
validates :username, uniqueness: true, if: -> { self.username.present? }
def build_user_profile
end
end
class-UserProfile{self.username.present?}
def生成用户配置文件
结束
结束
我的观点(designe/registrations/new.html.erb)
注册
资源名称:url=>注册路径(资源名称))do | f |%>
正确%>
正确%>
打开页面时,出现以下错误:
未定义的方法“username”
我对关联还比较陌生,不知道为什么:用户名没有被识别(或者正确的方法是什么)
我通过归属于:users,:User\u profiles
迁移在User(User\u profiles\u id)中添加了一个索引列。我亦打算凌驾遗赠注册控制员
似乎无法加载这张该死的表格。感谢您的帮助 您应该将
用户名
包装到字段中,用于
块:
<%= f.fields_for :user_profile do |profile_form| %>
<div><%= profile_form.label :username %><br />
<%= profile_form.text_field :username, :autofocus => true %></div>
<% end %>
正确%>
它围绕表单
f
的特定模型创建一个范围。更多。谢谢!这消除了错误——但是表单不会显示在页面上。有什么想法吗?没有,没有任何跟踪或错误信息。无论如何,使用接受
的在{u创建:构建{u配置文件
之后)来创建用户{u配置文件
),这或多或少会对您有所帮助。令人惊讶的是,这就是问题所在。一旦我删除接受
的嵌套属性,它就出现了。谢谢
<%= f.fields_for :user_profile do |profile_form| %>
<div><%= profile_form.label :username %><br />
<%= profile_form.text_field :username, :autofocus => true %></div>
<% end %>