Ruby 将哈希作为强参数传递并保存在DB中

Ruby 将哈希作为强参数传递并保存在DB中,ruby,forms,hash,ruby-on-rails-5,Ruby,Forms,Hash,Ruby On Rails 5,当我创建/更新一个记录时,我得到以下输出 Processing by ContactsController#create as HTML Parameters: {..."custom_fields_attr"=>{"1"=>"testing cft", "2"=>"", "3"=>"", "4"=>&q

当我创建/更新一个记录时,我得到以下输出

Processing by ContactsController#create as HTML
  Parameters: {..."custom_fields_attr"=>{"1"=>"testing cft", "2"=>"", "3"=>"", "4"=>""}}, "commit"=>"Create"}
但是,它似乎没有保存到数据库,当记录保存时,它会影响三个表
联系人
自定义字段
自定义字段值
,联系人表很好,但其他两个表没有更新,因此,散列的键是来自
custom\u fields
的字段,该值存储或更新在
custom\u field\u values

下面是表单生成的内容


测试
我的控制器中有以下强参数

自定义字段属性:{}

在每个模型中,保存后都有
:保存自定义字段\u attr
方法在
应用程序记录中,因此我尝试将其直接添加到模型中,并得到
未经允许的参数:1
,因此在我看来,保存后的
似乎没有被触发

在我实现强参数之前,我们在
new中有以下内容。在控制器中创建
方法

def新建
所有参数都允许
@coontact=Contact.new(参数[:Contact])
结束
all_params_allowed
application_controller
中的一个方法,因此我目前正在尝试将
联系人_controller
移动到强参数,除了
自定义字段_attr
散列外,其他一切都正常

contact.rb
(删除了一些不相关的方法)

上面提到的
应用程序记录中的方法如下

def save_custom_fields_attr
    if @posted_custom_fields
      values_h = self.custom_field_values_non_assoc_h

      context = nil
      context = self.get_custom_field_record_type_context if self.respond_to?('get_custom_field_record_type_context', true)

      self.class.custom_fields(context).each do |cf|
        posted_value = @posted_custom_fields[cf.id.to_s]

        if cf.input_type == 'time' && posted_value.respond_to?('strip', true)
          posted_value = posted_value.strip
        end

        cfv = values_h[cf.id.to_s]

        if !cfv && posted_value.present?
          cfv = CustomFieldValue.new({
             custom_field_id: cf.id,
             record_type: self.custom_field_record_type,
             record_id: self.id
          })
        end

        if posted_value.present?
          cfv.value = posted_value
          cfv.save
        elsif cfv
          cfv.destroy
        end
      end
    end
  end

我不明白你在这里想干什么?你想将这个散列
{“1”=>“testing cft”,“2”=>,“3”=>,“4”=>“}
保存在一列中吗?是的@Mshka该散列的内容进入
自定义字段值表中,所有的魔法都是在模型中使用“after\u save:save\u custom\u fields\u attr”来完成的,因此我似乎需要触发它。我会更新问题。更新,希望对@Mshkathanks有帮助!您还可以添加model contact.rb和控制器的内容吗?更新了问题@Mshka
class ContactsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_contact, only: [:show, :edit, :update, :remove_contact]
  respond_to :json, :html

  helper_method :sort_column, :sort_direction

  ...

  def index
    if params[:search].present?
      @contacts = Contact.search(params[:qs], params[:search]).order(sort_column + ' ' + sort_direction).paginate(page: params[:page], per_page: 20)
    else
      @contacts = Contact.all.order(sort_column + ' ' + sort_direction).paginate(page: params[:page], per_page: 20)
    end
  end

  def set_section
    @section = 'crm'
  end

  protected :set_section

  ...

  def show
    @contact_complaints = Event.where(complaint: true, contact_id: @contact.id).order('id desc').limit(10)
    @contact_leads = Event.where(lead: true, contact_id: @contact.id).order('id desc').limit(10)
    @contact_events = Event.where(complaint: false, lead: false, contact_id: @contact.id).order('id desc').limit(10)
    @contact_quotes = SalesQuote.where(contact_id: @contact.id).order('id desc').limit(10)
    @contact_sales_orders = SalesOrder.where(contact_id: @contact.id).order('id desc').limit(10)
    @contact_opportunities = @contact.get_contact_opportunities()
    @contact_estimates = Estimate.where(contact_id: @contact.id).order('id desc').limit(10)
    @support_tickets = SupportTicket.where(contact: @contact).limit(10)
    @support_tickets = SupportTicket.where(contact: @contact).limit(10)
  end

  def new
    @contact = Contact.new
    @tags = ActsAsTaggableOn::Tag.all

    if params[:l_tel]
      @contact.phone = params[:l_tel]
    end
    if params[:customer] && customer = Customer.find(params[:customer])
      @contact.customer = customer
    end
    if params[:supplier] && supplier = Supplier.find(params[:supplier])
      @contact.supplier = supplier
    end

    render(action: 'new_remote', layout: false) if request.xhr?
  end

  def edit
    @tags = ActsAsTaggableOn::Tag.all
  end

  def create
    @contact = Contact.new(contact_params)
    @contact.additional_validation = true

    if @contact.save
      if request.xhr?
        render :create_remote, layout: false
      else
        render :index, notice: 'Contact was successfully created.'
      end
    else
      if request.xhr?
        render :new_remote, layout: false
      else
        render :new
      end
    end
  end

  def update
    @contact.additional_validation = true
    @tags = ActsAsTaggableOn::Tag.all

    if @contact.update(contact_params)
      flash[:notice] = 'Contact was successfully updated.'
      redirect_to contacts_path
    else
      render :edit
    end
  end

  ...

  protected

  ...

  def set_contact
    @contact = Contact.find(params[:id])
  end

  private

  def sort_column
    Contact.column_names.include?(params[:sort]) ? params[:sort] : 'id'
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
  end

  def contact_params
    params.require(:contact).permit(
      :customer_account_id,
      :supplier_account_id,
      :other_information,
      :prospect_strength,
      :business_email,
      :private_email,
      :date_of_birth,
      :second_phone,
      :company_name,
      :contact_type,
      :assigned_to,
      :web_address,
      :created_at,
      :updated_at,
      :salutation,
      :created_by,
      :updated_by,
      :address_1,
      :address_2,
      :address_3,
      :address_4,
      :job_title,
      :postcode,
      :obsolete,
      :mobile,
      :spouse,
      :phone,
      :name,
      :url,

      tag_list: [],
      custom_fields_attr: {}
    )
  end
end
def save_custom_fields_attr
    if @posted_custom_fields
      values_h = self.custom_field_values_non_assoc_h

      context = nil
      context = self.get_custom_field_record_type_context if self.respond_to?('get_custom_field_record_type_context', true)

      self.class.custom_fields(context).each do |cf|
        posted_value = @posted_custom_fields[cf.id.to_s]

        if cf.input_type == 'time' && posted_value.respond_to?('strip', true)
          posted_value = posted_value.strip
        end

        cfv = values_h[cf.id.to_s]

        if !cfv && posted_value.present?
          cfv = CustomFieldValue.new({
             custom_field_id: cf.id,
             record_type: self.custom_field_record_type,
             record_id: self.id
          })
        end

        if posted_value.present?
          cfv.value = posted_value
          cfv.save
        elsif cfv
          cfv.destroy
        end
      end
    end
  end