在表单的参数前添加字符串 [英] Adding a string in front of a parameter for a form
问题描述
我想在表单上的参数前面添加一个字符串,以便当用户在表单上提交某些内容时,它会发布到外部 API 并且我的客户可以登录到 freshdesk.com,而不是说 BOB
,它会说Hello from BOB
.
I want to add a string in front of a paramemter on my form so that when the user submits something on the form, it posts to an external API and my client can log in to freshdesk.com, and instead of saying BOB
, it will say Hello from BOB
.
Hello from [:username]
我认为这是尝试过的:
= f.text_field "Hello From, #{:username}"
但它不起作用.我也尝试使用一个值:
but it does not work. I also tried to use a value:
= f.text_field :subject, value: "Hello From, #{:username}"
但这也不起作用.这是我的表格:
but that does not work either. Here is my form:
= 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
推荐答案
你的视图应该有简单的字段,没有魔法.我们将使用 Form 类来做复杂的事情.
Your view should have straightforward fields with no magic. We'll use the Form class to do the complicated stuff.
= f.text_field :subject
对 post_tickets 的方法调用不需要接收 params
,因为 Form 对象已经用 params
值初始化.另外,我认为您不应该发布票证,除非对象有效,对吗?
The method call to post_tickets does not need to receive the params
because the Form object has already been initialized with the params
values. Also, you shouldn't post the ticket, I think, unless the object is valid, right?
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
您的表单模型应该负责修改 :subject 参数以包含前缀:
Your Form model should be responsible for modifying the :subject parameter to include the prefix:
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
这样,Form 对象具有用户提交的正确值,但您已经覆盖了发布的主题,以便它将返回您想要的组合字符串,带有Hello from..."
That way, the Form object has the correct values as submitted by the user, but you've overwritten the posted subject so that it will return the combined string you want, with "Hello from..."
在 post_tickets 中,通过已初始化 Form 对象的属性直接引用您要发送的参数.对于 subject
,您将改为发送组合值.
In post_tickets, reference directly the parameters you want to send, via the attributes with which the Form object has been initialized. In the case of subject
, you'll send a combined value instead.
请注意,我已经重写了您的初始化以使用更经典的属性设置方法.
Please note, I've rewritten your initialize to use a more classic attribute setting method.
想法,问题?
这篇关于在表单的参数前添加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!