Rails 4 - 使用嵌套表单设计 - fields_for:符号问题

时间:2015-08-10 17:22:58

标签: ruby-on-rails ruby forms devise nested-forms

我尝试使用devise创建用户,我想在sign_up上创建business_detail(一对一的关系)。

我在模型中有以下关系。

user.rb

has_one :business_detail, dependent: :destroy
accepts_nested_attributes_for :business_detail

business_detail.rb

belongs_to :user

在表格中,我使用了以下代码:

= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
  = f.fields_for :business_detail do |b|

这里的问题是表单没有呈现,除非我使用复数形式(:business_details

如果我使用它,我就无法创建对象,因为模型是以单数形式命名的,我无法在fields_for上传递对象,因为那时我不会成为能够使用设计创建创建对象。

registrations_controller 中我已添加:

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) do |u|
    u.permit(:email, :password, :password_confirmation, :business, :name, :occupation, :gender, business_detail: [:city, :industry, :postcode, :photo, :phone])
  end
end

如果我使用复数形式(在表单内部和 configure_permitted_pa​​rameters 方法内),params看起来像这样:

{"utf8"=>"✓", "authenticity_token"=>"uH5axM94yiKH5rbVaAyZHzlHD6zoErSPkR5y7wAURhA=", "user"=>{"name"=>"", "password"=>"12345567", "password_confirmation"=>"123123", "business"=>"1", "business_details"=>{"address_line_1"=>"", "address_line_2"=>"", "city"=>"", "country"=>"", "postcode"=>"", "industry"=>"", "phone"=>""}, "email"=>"asdasdasd@aa.a"}, "commit"=>"Create Business Account", "controller"=>"registrations", "action"=>"business_create", "format"=>"user"}

和设计 build_resource 方法中的哈希是

{"email"=>"asdasdasd@aa.a", "password"=>"12345567", "password_confirmation"=>"123123", "business"=>"1", "name"=>"", "business_details"=>{"city"=>"", "industry"=>"", "postcode"=>"", "phone"=>""}}

所以,我认为使用单数形式发送符号可以解决我的问题。

我已尝试将模型添加到 config / initializers / inflections.rb 内的变形列表中:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.uncountable %w( business_detail )
end

但它不起作用。

有人建议吗?

0 个答案:

没有答案