Wednesday, 11 September 2013

RAILS - tacking on a message when form is submitted

RAILS - tacking on a message when form is submitted

I can't seem to figure this out or find a solution to this
anywhere...which is crazy to me since i feel like its pretty common and
simple
I want to add a little message that my client will see when the user sends
them a request through the form and then it goes to an external API where
they can see created tickets.
so right now my client sees
John Doe
but i want them to see
Web inquiry from John Doe
So i need to send the "Web inquiry from" part through the form
i've tried to interpolate it in the form
= f.text_field "Web inquiry from #{:subject}"
that didnt work
i've tried to add a value (not the way i want to go but i tried it anyway)
= f.text_field :subject, value: "Web inquiry from #{f.object.subject}"
that did not work either
i've tried to place it in the model
def post_tickets(params)
client.subject = "Hello from, " + client.subject
end
I'm new to rails so if you could be specific as possible that would be
helpful...please dont say just do it in the controller.....thank you in
advanced
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'
here is my 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
here is my model
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

No comments:

Post a Comment