0

フォームのパラメーターの前に文字列を追加して、ユーザーがフォームで何かを送信すると、外部 API に投稿され、クライアントが freshdesk.com にログインできるようにしBOBます。Hello from BOB.

Hello from [:username]

私は私の見解でこれを試しました:

= f.text_field "Hello From, #{:username}" 

しかし、それは機能しません。私も値を使用しようとしました:

= f.text_field :subject, value: "Hello From, #{:username}"

しかし、それもうまくいきません。ここに私のフォームがあります:

= 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 "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'

これが私のコントローラーです:

def new
  @contacts = Form.new
end

def create
  @contacts = Form.new(params[:contacts])
  @contacts.post_tickets(params[:contacts])
  if @contacts.valid?
    flash[:success] = "Message sent! Thank you for conacting us."
    redirect_to new_contact_path
  else
    flash[:alert] = "Please fill in the required fields"
    render action: 'new'
  end
end

これは私のモデルからのものです

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, 
            :custom_field_name_28445, :custom_field_company_28445, :description, 
            :custom_field

  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|
      @attributes = attributes
    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 read_attribute_for_validation(key)
      @attributes[key]
    end

    def post_tickets(params)
      client.post_tickets(params)
    end

    def persisted?
      false
    end
   end
4

2 に答える 2

2

ビューには、魔法のない単純なフィールドが必要です。Form クラスを使用して、複雑なことを行います。

= f.text_field :subject

paramsForm オブジェクトはすでに値で初期化されているため、post_tickets へのメソッド呼び出しは を受け取る必要はありませんparams。また、オブジェクトが有効でない限り、チケットを投稿するべきではないと思いますよね?

def create
  @contacts = Form.new(params[:contacts])
  if @contacts.valid?
    @contacts.post_tickets
    flash[:success] = "Message sent! Thank you for contacting us."
    redirect_to new_contact_path
  else
    flash[:alert] = "Please fill in the required fields"
    render action: 'new'
  end
end

Form モデルは、:subject パラメータを変更してプレフィックスを含める必要があります。

class Form
  # Some code omitted

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

  def post_tickets
    client.post_tickets({
      :whatever_fields => whatever_attribute, 
      :username => username, 
      :subject => "Hello from, #{subject}", 
      :email => email, 
      :description => description
    })
  end

end

そうすれば、フォーム オブジェクトはユーザーが送信した正しい値を持ちますが、投稿された件名を上書きして、必要な結合文字列を "Hello from..." で返すようにします。

post_tickets では、フォーム オブジェクトが初期化された属性を介して、送信するパラメーターを直接参照します。の場合はsubject、代わりに結合された値を送信します。

より古典的な属性設定方法を使用するように初期化を書き直したことに注意してください。

考え、質問?

于 2013-09-11T17:25:40.780 に答える