0

这是我处理我的表格的模型

class Form
  include ActiveModel::Validations
  include ActiveModel::Conversion
  include ActiveModel::Translation
  extend  ActiveModel::Naming

  attr_accessor :config, :client, :subject, :email, :custom_field, :phone_number_28445, :company_28445, :description

  validates_presence_of :subject, :message => '^Please enter your name'
  validates_presence_of :description, :message => '^Question(s), and/or feedback can not be blank'
  validates :email, presence: true  
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end

    self.config = YAML.load_file("#{Rails.root}/config/fresh_desk.yml")[Rails.env]
self.client = Freshdesk.new(config[:url], config[:api_key], config[:password])
  end

  def post_tickets
    client.post_tickets({
      :subject => "Web Inquiry From, #{subject}", 
      :email => email,
      :custom_field => {:phone_number_28445 => phone_number_28445},
      :description => description
     }) 
  end

  def persisted?
    false
  end
end

在我的 post_tickets 方法中它工作正常....问题出在

:custom_field_phone_number_28445 => custom_field_phone_number_28445

我知道这一点,因为一旦我把它从表格中取出......当我把它重新添加进去时,我得到一个

   Completed 500 Internal Server Error in 1222ms


   Freshdesk::ConnectionError (Connection to the server failed. Please check hostname):
   lib/freshdesk.rb:77:in `rescue in block in fd_define_post'
   lib/freshdesk.rb:70:in `block in fd_define_post'
   app/models/form.rb:25:in `post_tickets'
   app/controllers/website/contacts_controller.rb:8:in `create'
   config/initializers/quiet_assets.rb:8:in `call_with_quiet_assets'

这是它工作时发布的内容

 Parameters: 
   {"utf8"=>"✓", "authenticity_token"=>"g91E0jYezX+5gFAe6R/hUPs9b+UqS1uMqki4rE6xU28=", 

   "contacts"=>{"subject"=>"ricky ahn ", 
   "email"=>"ricky@onehouse.net", 
   "custom_field"=>{"phone_number_28445"=>"123-123-1234"}, 
   "description"=>"test"}, 

   "commit"=>"Submit"}

   Redirected to http://localhost:3000/contacts/new
   Completed 302 Found in 2142ms (ActiveRecord: 0.0ms)

这是它不起作用时发布的内容

   Parameters: {"utf8"=>"✓", "authenticity_token"=>"g91E0jYezX+5gFAe6R/hUPs9b+UqS1uMqki4rE6xU28=",
    "contacts"=>{"subject"=>"ricky ahn ", 
    "email"=>"ricky@onehouse.net", 
    "custom_field"=>{"phone_number_28445"=>"123-123-1234"}, 
    "description"=>"test"}, "commit"=>"Submit"}
    Completed 500 Internal Server Error in 841ms

我不知道我在这里做错了什么......

这是 html/haml

= form_for(:contacts, url: contacts_path) do |f|
  = f.error_messages
  = f.label :subject, "Name"
  %span{style: 'color: red'} *
  = f.text_field :subject, class: "text_field width_100_percent"
  %br
  %br    
  = f.label "Email"
  %span{style: 'color: red'} *
  %br    
  = f.email_field :email, class: "text_field width_100_percent"
  %br
  %br
  = f.label "Phone"   
  %br    
  = f.fields_for :custom_field do |custom_field|
    = custom_field.text_field :phone_number_28445, class: "text_field width_100_percent"
  %br
  %br
  = f.label "Question(s), and/or feedback"
  %span{style: 'color: red'} *
  %br
  = f.text_area :description, class: "text_field width_100_percent", style: 'height: 100px;'
  %br
  %br
  = f.submit "Submit", class: 'btn btn-warning'
4

1 回答 1

1

您指定此名称是有原因的吗?导致问题的值是唯一一个带有覆盖表单名称的值。

= f.text_field :custom_field_phone_number_28445,
   name: 'contacts[custom_field][phone_number_28445]',
   class: "text_field width_100_percent"

不应该只是:

= f.text_field :custom_field_phone_number_28445,
   class: "text_field width_100_percent"

所有其他属性都没有嵌套在表单上的子名称中......我猜这个值没有在表单模型中设置,最终为零,并且链下的某些东西对此很生气。

于 2013-09-12T18:36:17.710 回答