从一种形式获取信息,保存到另一种模型
Getting info from one form, saving to another model
我有2个模型
- 用户
- 个人资料(belongs_to 用户)
我在配置文件模型中有 'username'。我想取 'username' 的值
使用新表单并将其保存到配置文件的用户名。我怎样才能做到这一点。请建议代码。
试试这个
将这些添加到您的 Gemfile 中
gem 'simple_form'
gem 'nested_form'
在你的用户模型中添加这个
accepts_nested_attributes_for :profile
在您的表格中 simple_nested_form_for
= f.simple_fields_for :profile do |p|
= p.input :username
在你的用户控制器中
添加这个以允许配置文件参数
def user_params
params.require(:user).permit(
#User Attribute Names,
profile_attributes: [#Profile atrribute names]
)
我有2个模型
- 用户
- 个人资料(belongs_to 用户)
我在配置文件模型中有 'username'。我想取 'username' 的值 使用新表单并将其保存到配置文件的用户名。我怎样才能做到这一点。请建议代码。
试试这个
将这些添加到您的 Gemfile 中
gem 'simple_form'
gem 'nested_form'
在你的用户模型中添加这个
accepts_nested_attributes_for :profile
在您的表格中 simple_nested_form_for
= f.simple_fields_for :profile do |p|
= p.input :username
在你的用户控制器中 添加这个以允许配置文件参数
def user_params
params.require(:user).permit(
#User Attribute Names,
profile_attributes: [#Profile atrribute names]
)