フォームのパラメーターの前に文字列を追加して、ユーザーがフォームで何かを送信すると、外部 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