为什么没有参数填充类的新实例?

时间:2014-09-11 16:32:18

标签: ruby-on-rails ruby

我正在将表单提交到CorporateContactFormsController。我可以看到参数正在传递,但它们没有被添加到实例化的类中。

  def create
      @contact_form = CorporateContactForm.new(corporate_contact_form_params) 
    raise
  end

  private 

  def corporate_contact_form_params
    params.require(:corporate_contact_form).permit(:firstname, :surname, :contact_number, :email, :company_name, :method_of_payment, :number_of_employees, :comments, :contact_type)
  end

这是来自better-errors gem

>> corporate_contact_form_params
=> {"firstname"=>"qwertyui", "surname"=>"wertyuio", "contact_number"=>"wertyuio", "email"=>"qwdqwd@iugh.com", "company_name"=>"wqedfwefwef", "method_of_payment"=>"Other", "number_of_employees"=>"1-50", "comments"=>"qwdqwdqwdqwdqwdqwdwqd", "contact_type"=>"employee"}

>> @contact_form = CorporateContactForm.new(corporate_contact_form_params) 
=> #<CorporateContactForm id: nil, contact_type: nil, firstname: nil, surname: nil, contact_number: nil, email: nil, company_name: nil, company_website: nil, position_in_company: nil, location: nil, number_of_employees: nil, method_of_payment: nil, comments: nil, created_at: nil, updated_at: nil>

为什么没有填充该类新实例的参数?

我正在使用rails 4.0

更新

模型

class CorporateContactForm < ActiveRecord::Base

    EMPLOYEE = 'employee'
    EMPLOYER = 'employer'
    CONTACT_TYPE = [EMPLOYEE, EMPLOYER]

    ONE_TO_FIFITY = "1-50"
    FIFTY_TO_ONEHUNDRED = "50-100"
    ONEHUNDRED_OR_MORE = "100+"
    EMPLOYEE_COUNT = [ONE_TO_FIFITY, FIFTY_TO_ONEHUNDRED, ONEHUNDRED_OR_MORE]

  OTHER = "Other"
  COMPANY = "Company funded"
  METHOD_OF_PAYMENT = [COMPANY, OTHER]

  validates :contact_type, inclusion: CONTACT_TYPE, presence: true
  validates :number_of_employees, inclusion: EMPLOYEE_COUNT, presence: true
  validates :firstname, presence: :true
  validates :email, presence: :true
  validates :company_name, presence: :true
  validates :email, presence: :true
  validate :blanks_to_nils

    private

  def blanks_to_nils
    blank_to_nil :surname
    blank_to_nil :contact_number
    blank_to_nil :position_in_company
    blank_to_nil :location
    blank_to_nil :method_of_payment
    blank_to_nil :comments
  end

0 个答案:

没有答案