Ricky Ahn
Ricky Ahn

Reputation: 191

Rails - Form not posting in external API due to parameter

heres my model thats handling my form

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

in my post_tickets method it works fine....the problem is with the

:custom_field_phone_number_28445 => custom_field_phone_number_28445

I know this because as soon as i take that out the form posts.......when i add it back in i get a

   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'

heres whats getting posted when it works

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

   "contacts"=>{"subject"=>"ricky ahn ", 
   "email"=>"[email protected]", 
   "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)

heres what gets posted when it doesent work

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

i dont know what i'm doing wrong right here....

here is the 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'

Upvotes: 0

Views: 268

Answers (1)

Nick Veys
Nick Veys

Reputation: 23949

Did you specify this name for a reason? The value that's causing a problem is the only one w/an overridden form name.

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

Shouldn't it just be:

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

All the other attributes aren't nested into a sub-name on the form... I'm guessing that value isn't being set in the form model, ends up nil, and something down the chain is mad about that.

Upvotes: 1

Related Questions