从一个表单获取信息,保存到另一个模型

时间:2015-01-22 16:16:09

标签: ruby ruby-on-rails-4

我有2个模特

  1. 用户
  2. 个人资料(belongs_to用户)
  3. 我有'用户名'在Profile模型中。我想要使​​用'用户名'在 用户新表单并将其保存到Profile的用户名。我怎样才能做到这一点。请建议代码。

1 个答案:

答案 0 :(得分:1)

试试这个

在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]
  )
相关问题