以RoR简单格式添加新字段将提供ActionView :: Template :: Error

时间:2019-01-04 02:16:16

标签: ruby-on-rails simple-form

我们希望在表单中添加一个新的隐藏字段,但是每次我添加一个新字段时(甚至复制并粘贴一个有效的字段并在末尾添加一个数字),我们都会收到错误消息,

ActionView::Template::Error (undefined method `hidden_gclid' for #<Wizard::Lead::Step2:0x00000004d859d8>):

请让我知道您想查看哪个代码。我尝试阅读一些文档here,并认为可能需要添加数据库列,但不确定。

感谢大家的帮助,对于造成的延迟,我深表歉意。我没收到  通知通知。我有一个新问题。创建新字段后,我们会收到此错误,

F, [2019-01-09T05:08:28.887941 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e]   
F, [2019-01-09T05:08:28.888395 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e] NameError (undefined local variable or method `hidden_gclid' for #<Lead:0x0000000527c4a0>):
F, [2019-01-09T05:08:28.888449 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e]   
F, [2019-01-09T05:08:28.888487 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e] app/models/lead.rb:91:in `sales_force_info'

lead.rb

# This model represents an User's (customer) submission through the form
class Lead < ApplicationRecord
  validates :address, :lat, :lng, presence: true

  belongs_to :user, optional: true

  has_and_belongs_to_many :characteristics

  has_many :offers, inverse_of: :lead, dependent: :destroy

  scope :newest_first, (-> { order(created_at: :desc) })

  enum pool_type: %i[
    in_ground above_ground none_or_community
  ]

  enum kitchen_condition: %i[
    great_kitchen typical_kitchen needs_work_kitchen
  ]
  enum bathroom_condition: %i[
    great_bathroom typical_bathroom needs_work_bathroom
  ]

  enum timeline_to_sell: %i[
    asap 2_4_weeks 4_6_weeks few_months just_curious
  ]

  enum looking_for_another: %i[
    yes already_found no
  ]

  enum reasons_for_selling: %i[
    upgrading relocating downsizing retiring selling_investment
  ]

  enum offer_status: %i[
    pending sent accepted declined closed
  ]

  attr_accessor :hidden_gclid  

  def has_basic_information?
    if bedrooms.present? || bathrooms.present? ||
       built_surface.present? || pool_type.present? ||
       kitchen_condition.present? || bathroom_condition.present? ||
       renovated.present? || renovated_spent.present? ||
       renovated_description.present?
      return true
    end

    false
  end

  def street
    (address.remove(', USA')&.split(",").count == 3) ? address.remove(', USA')&.split(",").first : "#{address.remove(', USA')&.split(",").first}, #{address.remove(', USA')&.split(",").second}"
  end

  def city
    address.remove(', USA')&.split(",").second_to_last
  end

  def state_code
    address.remove(', USA')&.split(",").last
  end

  def sales_force_info
    form = { 'oid' => 'xxxxxx', # roganization id
             'retURL' => 'http://placeholder.com',
             '00No0000009e5Lb' => 'Cash Offer',
             'country_code' => 'US',
             'first_name' => user&.first_name,
             'last_name' => user&.last_name,
             'phone' => user&.phone,
             'street' => street, #address
             'city' =>  city,
             'state_code' => state_code,
             'zip' => zip,
             'description' => "Check more lead info at www.placeholder.com/leads/#{id}",
             'email' => user&.email,
             'lead_source' => user&.how_about_us || "Web",
             '00N1N00000Oko3t' => bedrooms,
             '00N1N00000Oko3y' => bathrooms,
             '00N1N00000Oko43' => built_surface,
             '00N1N00000Oko48' => air_conditioner,
             '00N1N00000Oko4N' => roof_age,
             '00N1N00000Oko4h' => timeline_to_sell,
             '00N1N00000Oko4m' => looking_for_another,
             '00N1N00000Oko4r' => reasons_for_selling,
             '00N1N00000Oko4S' => renovated,
             '00N1N00000Oko4X' => renovated_spent,
             '00N1N00000Oko4c' => renovated_description,
             '00N1N00000Oko4w' => own_valuation,
             '00N1N00000PMKAo' => hidden_gclid
            }
    form
  end

  def send_info_to_sales_force
    puts "SENDING INFO TO SALES FORCE!"
    url = URI('https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8')
    form = sales_force_info
    res = Net::HTTP.post_form(url, form)
    puts res.body
  end
end

表格

:javascript
  window.onload = function getGclid() {        
             document.getElementById("00N1N00000PMKAo").value = (name = new RegExp('(?:^|;\\s*)gclid=([^;]*)').exec(document.cookie)) ? name.split(",")[1] : ""; }
.block.block-fill-height.px-0
  = render 'application/header', logo: 'logo-blue.png'
  .container.mt-2.pt-5
    .card.card-outline-primary
      %h3.card-header
        %ul.nav.nav-bordered
          %li.nav-item
            %a.nav-link{:href => "#"} Basics
          %li.nav-item
            %a.nav-link.active{:href => "#"} Sale
          %li.nav-item
            %a.nav-link{:href => "#"} Offer
      .card-block
        %h4.card-title Where should we send your offer?
        %p.card-text
          %strong Save your progress
          and join thousands of home owners who work with us every month.
        %hr
          %h5.mt-5 Create your account below:
          = simple_form_for User.new, url: '/users', method: 'POST', html: { class:'mt-4' } do |f|
            .form-group
              = f.label(:phone, 'Phone')
              = f.input_field(:phone, class: 'form-control', placeholder: 'Phone', required: true)
            .form-group
              = f.label(:first_name, 'First Name')
              = f.input_field(:first_name, class: 'form-control', placeholder: 'First Name')
            .form-group
              = f.label(:last_name, 'Last Name')
              = f.input_field(:last_name, class: 'form-control', placeholder: 'Last Name')
            .form-group
              = f.label(:email, 'Email')
              = f.input_field(:email, class: 'form-control', placeholder: 'Enter your Email')
            .form-group
              = f.label(:password, 'Password')
              = f.input_field(:password, class: 'form-control', placeholder: '6 characters minimum')
            .form-group
              = f.label(:how_about_us, 'Where did you hear about us?')
              %br
              = f.select :how_about_us, ['TV Commercial', 'Word of Mouth', 'Radio', 'Web', 'Letter/Postcard/Doorhanger',
                                     'Social Media', 'Telephone Call'], class: 'form-control', prompt: 'Please Select', required: true
            %hr
              = hidden_field_tag(:hidden_gclid, "", :id => "00N1N00000PMKAo")
              = hidden_field_tag(:current_step, 'step3')
              = f.submit 'Next', class: 'btn btn-success btn-lg'

看起来我们的表单现在没有任何错误,但是我们需要将隐藏字段ID 00N1N00000PMKAo发送给salesforce,但不是。

1 个答案:

答案 0 :(得分:0)

由于simple_form依赖于模型实例,因此这里有两个选择:

  • 将数据库列添加到相应的表
  • 使用虚拟属性

看起来您不想将值存储在数据库中,在这种情况下,只需将attr_accessor :hidden_gclid添加到模型中即可。之后,您可以像在控制器操作中的普通属性一样使用它。