gpt4 book ai didi

ruby-on-rails - RAILS - 提交表单时添加消息

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:16 25 4
gpt4 key购买 nike

<分区>

我似乎无法弄清楚这个问题或在任何地方找到解决方案......这对我来说很疯狂,因为我觉得它非常普遍和简单

我想添加一条小消息,当用户通过表单向他们发送请求时,我的客户会看到这条消息,然后它会转到外部 API,他们可以在其中看到创建的工单。

所以现在我的客户看到了

 John Doe 

但我想让他们看到

 Web inquiry from John Doe

所以我需要通过表单发送“Web inquiry from”部分

我试着将它插入到表单中

 = f.text_field "Web inquiry from #{:subject}"

那没用

我已经尝试添加一个值(不是我想要的方式,但我还是试过了)

 = f.text_field :subject, value: "Web inquiry from #{f.object.subject}"

那也没用

我试着把它放在模型中

 def post_tickets(params)
client.subject = "Hello from, " + client.subject
end

我是 Rails 的新手,所以如果你能尽可能具体那会有帮助......请不要说只是在 Controller 中......谢谢高级

这是我的表格

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

这是我的 Controller

 class Website::ContactsController < Website::WebsiteApplicationController
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
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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com