在Rails 4中以嵌套形式更新子对象

时间:2014-12-01 14:53:31

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

我当前的devise配置有一个用户对象,它有一个表继承结构,可以分解为另外两种用户类型(其中一种是业务)。我想要更新的业务的子对象称为'supp_forms'。当我尝试更新记录时,我在终端中收到以下错误。我正在使用nested_form_for gem来处理我的嵌套表单。

Unpermitted parameters: supp_form_attributes

但是,传递的参数看起来是正确的(传递的数据是我在表单中编辑过的数据)。

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"XX", "business"=>{"supp_form_attributes"=>{"work_phone_number"=>"(906) 790-6969 x69696", "business_address"=>"1 XXXX st", "business_postal_code"=>"L0R 1K2", "business_city"=>"Oria", "business_province"=>"ON", "employee_count"=>"5", "id"=>"96"}}, "commit"=>"Update Business"}

我的更新表格如下所示。 的 business_profile.html.erb

<%= nested_form_for @user, url: business_registration_path do |f| %>
  <%= f.fields_for :supp_form do |supp_form| %>
    <%= supp_form.label :work_phone_number %>
    <%= supp_form.text_field :work_phone_number %>
    <%= supp_form.label :business_address %>
    <%= supp_form.text_field :business_address %>
    <%= supp_form.label :business_postal_code %>
    <%= supp_form.text_field :business_postal_code %>
    <%= supp_form.label :business_city %>
    <%= supp_form.text_field :business_city %>
    <%= supp_form.label :business_province %>
    <%= supp_form.text_field :business_province %>
    <%= supp_form.label :employee_count %>
    <%= supp_form.text_field :employee_count %>
   <% end %>
  <%= f.submit %>
<% end %>

business.rb

class Business < User
  # Associations
  has_one :supp_form
  has_many :loan_applications
  has_many :transactions
  has_many :listing_information_forms

  # Nested attributes
  accepts_nested_attributes_for :supp_form
end

supp_form.rb

class SuppForm < ActiveRecord::Base
  # Associations
  belongs_to :business
end

supp_forms_controller.rb

class SuppFormsController < ApplicationController
    before_filter :authenticate_user!

    def edit
      @user = User.current_user
    end 

    def update
      @user = current_user
      @suppform = @user.supp_form
      if @suppform.update_attributes(supp_form_params)
        business_supp_form_path(@user)
      else
        render 'edit'
      end
    end 

    private

      def supp_form_params
        params.require(:supp_form).permit(:business_id, :title, :loan_agreement_authorization, :first_name, :last_name, :applicant_role, :work_phone_number, :business_address, :business_postal_code,:business_city, :business_name, :years_in_business, :legal_structure, :ownership, :business_industry, :employee_count, :mobile_phone_number, :business_province, :business_country)
      end
end

business_account_controller.rb

class BusinessAccountController < ApplicationController
  before_filter :authenticate_user!

  def business_profile
    @user = current_user
  end
end

registrations_controller.rb(针对企业)

class Businesses::RegistrationsController < Devise::RegistrationsController
  before_filter :update_sanitized_params

  def edit
    @user = current_user
    super
  end 

  def update
    @user = current_user
    super
  end

private

  def update_sanitized_params
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:email, :password, :password_confirmation, :type, :confirmed_at, :business_name, :terms, :railsid, :terms_of_service, supp_form_attributes: [:business_id, :title, :loan_agreement_authorization, :first_name, :last_name, :work_phone_number, :applicant_role, :business_address, :business_postal_code, :business_city, :business_name, :years_in_business, :legal_structure, :ownership, :business_industry, :employee_count, :mobile_phone_number, :business_province, :business_country])}
  end

end

1 个答案:

答案 0 :(得分:2)

请尝试按照以下方式操作,它适用于我,并希望它对您有所帮助。

class RegistrationsController < Devise::RegistrationsController
  def create
    devise_parameter_sanitizer.for(:sign_up) << { profile_attributes: [:first_name, :last_name] }
    super
  end
end