I am using gem mail_form to handle contact in a Rails app, and I have 6 different contact forms.
I put a hidden_field_tag in the form and pass the desired subject as a variable. In the html, the value is there but the email arrives with (no subject). What can I be doing wrong?
In controllers
def where_to_buy
#contact = Contact.new
#the_subject = "Where to buy"
end
In contact form
= form_for #contact do |f|
= render "form", f: f
= f.text_area :message
.hide
= f.text_field :nickname, hint: 'Leave this field empty!'
= hidden_field_tag :mail_subject, #the_subject
= f.submit "Send Message"
In model
class Contact < MailForm::Base
attribute :mail_subject
attribute :first_name, validate: true
attribute :last_name, validate: true
attribute :message, validate: true
attribute :nickname, captcha: true
def headers
{
subject: %(#{mail_subject}),
to: "jorge#email123.com",
from: %("#{first_name} #{last_name}" <#{email}>)
}
end
end
Output html in chrome:
<input type="hidden" name="mail_subject" id="mail_subject" value="Where to buy">
Instead of:
= hidden_field_tag :mail_subject, #the_subject
You will want to use:
= f.hidden_field :mail_subject, value: #the_subject
If you inspect the parameters that are logged into your development.log you will see why.
When you use hidden_field_tag the mail_subject is defined as its own independent parameter and will not be included in the contact hash. You'll have something like:
params = { "contact" => { "message" => "text here", ... }, "mail_subject => "Where to buy" }
But when you use f.hidden_field the mail_subject will be included in the contact hash. You'll have something like:
params = { "contact" => { "message" => "text here", "mail_subject => "Where to buy", ... } }
And then when you call Contact.new(params[:contact]) the new contact object will get the mail_subject value.
Related
So I have a form that I'm trying to allow the user to copy over text from billing to shipping address. Unfortunately at the moment it's automatically saving billing address in the shipping address.
My form looks like the following (albeit truncated for massive amount of fields):
=form_for #customer, url: create_customer_path, html: {class: 'new-customer} do |f|
.row
.col-md-4
= f.label :first_name
.col-md-8
= f.text_field :first_name, class: 'form-control', required:true
.row
.col-md-4
= f.label :billing_address1
.col-md-8
= f.text_field :billing_address1, class: 'form-control', required:true
.row
.col-md-12
= f.check_box :shipping_is_billing
= f.label :shipping_is_billing, 'Same as billing address'
.row
.col-md-4
= f.label :shipping_address1
.col-md-8
= f.text_field :shipping_address1
CustomersController
class CustomersController < ApplicationController
def new
#customer = CustomerForm.new
end
def create
#customer = CustomerForm.new(customer_params)
if #customer.save
redirect_to customer_success_path
else
render 'new'
end
end
private
def customer_params
params.require(:customer_form).permit!.tap do |p|
p[:captcha_response] = params['g-recaptcha-response']
end
end
end
CustomerForm (truncated for massive fields)
class CustomerForm
include ActiveModel::Model
CUSTOMER_ATTRS = %w[
first_name
].freeze
ADDRESS_ATTRS = %w[
address1 address2 city state zip
].freeze
attr_accessor(*CUSTOMER_ATTRS)
attr_accessor(*ADDRESS_ATTRS.map { |attr| 'billing_' + attr })
attr_accessor(*ADDRESS_ATTRS.map { |attr| 'shipping_' + attr })
attr_accessor :confirm_email, :captcha_response, :shipping_is_billing
validates :first_name, presence: true
validates :billing_address1, presence: true
validates :shipping_address1, presence: true, unless: :shipping_is_billing
def save
return false unless valid?
persist!
end
private
def captcha_passes
captcha = Captcha.new
return if captcha.valid?(captcha_response)
errors.add(:captcha_response, 'is invalid')
end
def persist!
customer = Customer.new(attrs_for_customer)
customer.billing_address = CustomerAddress.new(attrs_for_address('billing_'))
customer.shipping_address = CustomerAddress.new(
attrs_for_address(shipping_is_billing ? 'billing_' : 'shipping_')
)
customer.save!
customer
end
def attrs_for_customer
Hash[
CUSTOMER_ATTRS.map { |attr| [attr, send(attr)] }
]
end
def attrs_for_address(prefix)
Hash[
ADDRESS_ATTRS.map { |attr| [attr, send(prefix + attr.to_s)] }
]
end
end
JS
app.newCustomer = () => {
function init() {
let check = document.querySelector('#customer_form_shipping_is_billing')
check.addEventListener('change', toggledUseBilling)
}
let toggledUseBilling = event => {
shippingFields().forEach(field => {
if(event.target.checked) {
field.value = null;
field.removeAttribute('required');
field.setAttribute('disabled', true);
} else {
field.setAttribute('required', true);
field.removeAttribute('disabled');
}
})
}
let shippingFields = () => {
let selectors = [
'#customer_form_shipping_address1',
'#customer_form_shipping_address2',
'#customer_form_shipping_city',
'#customer_form_shipping_state',
'#customer_form_shipping_zip',
]
return document.querySelectorAll(selectors.join(', '));
}
return init();
}
Under the persist! method I'm using the ternary operator on the checkbox to determine the attributes for the address to be billing/shipping. But it doesn't look like it's actually working. How do I grab from the form the checkbox being marked?
Here are the things I've tried:
Switched = f.check_box :shipping_is_billing to =check_box_tag :shipping_is_billing. Then I had to update my JS to make sure I was grabbing the right checkbox. This stores the shipping data but when checked the business data isn't being copied.
Attempted to switch attrs_for_address(shipping_is_billing ? 'billing_' : 'shipping_') to attrs_for_address(shipping_is_billing ? 'shipping_' : 'billing_'). If I use the check_box this will populate the data over to shipping but the checkbox becomes ineffectual.
Put a form_tag around the check_box_tag but this actually stripped out the check box
Changed the check_box to check_box_tag, updated my JS to look for the correct ID on the checkbox. Can save the shipping address if typed in but if checking the box it does not apply the billing address and I get prompts that the shipping address can't be blank
Did a binding.pry within the persist! method. Looks like shipping_is_billing is pulling a string which is hitting truthy. Changed to
customer.shipping_address = CustomerAddress.new(
attrs_for_address(shipping_is_billing == "1" ? 'billing_' : 'shipping_')
We are looking to add a new hidden field into our form but every time I add in a new field (even copy and pasting a field that works and just adding a number to the end) we get an error,
ActionView::Template::Error (undefined method `hidden_gclid' for #<Wizard::Lead::Step2:0x00000004d859d8>):
Please let me know which code you would like to see. I tried reading some documentation here and thought, I might need to add a database column but I'm not sure.
Thank you all for the help and I apologize for the delay. I didn't receive
notification on the comments. I have a new problem though. After getting the new field created we are getting this error,
F, [2019-01-09T05:08:28.887941 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e]
F, [2019-01-09T05:08:28.888395 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e] NameError (undefined local variable or method `hidden_gclid' for #<Lead:0x0000000527c4a0>):
F, [2019-01-09T05:08:28.888449 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e]
F, [2019-01-09T05:08:28.888487 #26599] FATAL -- : [437ed38a-c36c-4121-9608-385c43d0449e] app/models/lead.rb:91:in `sales_force_info'
lead.rb
# This model represents an User's (customer) submission through the form
class Lead < ApplicationRecord
validates :address, :lat, :lng, presence: true
belongs_to :user, optional: true
has_and_belongs_to_many :characteristics
has_many :offers, inverse_of: :lead, dependent: :destroy
scope :newest_first, (-> { order(created_at: :desc) })
enum pool_type: %i[
in_ground above_ground none_or_community
]
enum kitchen_condition: %i[
great_kitchen typical_kitchen needs_work_kitchen
]
enum bathroom_condition: %i[
great_bathroom typical_bathroom needs_work_bathroom
]
enum timeline_to_sell: %i[
asap 2_4_weeks 4_6_weeks few_months just_curious
]
enum looking_for_another: %i[
yes already_found no
]
enum reasons_for_selling: %i[
upgrading relocating downsizing retiring selling_investment
]
enum offer_status: %i[
pending sent accepted declined closed
]
attr_accessor :hidden_gclid
def has_basic_information?
if bedrooms.present? || bathrooms.present? ||
built_surface.present? || pool_type.present? ||
kitchen_condition.present? || bathroom_condition.present? ||
renovated.present? || renovated_spent.present? ||
renovated_description.present?
return true
end
false
end
def street
(address.remove(', USA')&.split(",").count == 3) ? address.remove(', USA')&.split(",").first : "#{address.remove(', USA')&.split(",").first}, #{address.remove(', USA')&.split(",").second}"
end
def city
address.remove(', USA')&.split(",").second_to_last
end
def state_code
address.remove(', USA')&.split(",").last
end
def sales_force_info
form = { 'oid' => 'xxxxxx', # roganization id
'retURL' => 'http://placeholder.com',
'00No0000009e5Lb' => 'Cash Offer',
'country_code' => 'US',
'first_name' => user&.first_name,
'last_name' => user&.last_name,
'phone' => user&.phone,
'street' => street, #address
'city' => city,
'state_code' => state_code,
'zip' => zip,
'description' => "Check more lead info at www.placeholder.com/leads/#{id}",
'email' => user&.email,
'lead_source' => user&.how_about_us || "Web",
'00N1N00000Oko3t' => bedrooms,
'00N1N00000Oko3y' => bathrooms,
'00N1N00000Oko43' => built_surface,
'00N1N00000Oko48' => air_conditioner,
'00N1N00000Oko4N' => roof_age,
'00N1N00000Oko4h' => timeline_to_sell,
'00N1N00000Oko4m' => looking_for_another,
'00N1N00000Oko4r' => reasons_for_selling,
'00N1N00000Oko4S' => renovated,
'00N1N00000Oko4X' => renovated_spent,
'00N1N00000Oko4c' => renovated_description,
'00N1N00000Oko4w' => own_valuation,
'00N1N00000PMKAo' => hidden_gclid
}
form
end
def send_info_to_sales_force
puts "SENDING INFO TO SALES FORCE!"
url = URI('https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8')
form = sales_force_info
res = Net::HTTP.post_form(url, form)
puts res.body
end
end
form
:javascript
window.onload = function getGclid() {
document.getElementById("00N1N00000PMKAo").value = (name = new RegExp('(?:^|;\\s*)gclid=([^;]*)').exec(document.cookie)) ? name.split(",")[1] : ""; }
.block.block-fill-height.px-0
= render 'application/header', logo: 'logo-blue.png'
.container.mt-2.pt-5
.card.card-outline-primary
%h3.card-header
%ul.nav.nav-bordered
%li.nav-item
%a.nav-link{:href => "#"} Basics
%li.nav-item
%a.nav-link.active{:href => "#"} Sale
%li.nav-item
%a.nav-link{:href => "#"} Offer
.card-block
%h4.card-title Where should we send your offer?
%p.card-text
%strong Save your progress
and join thousands of home owners who work with us every month.
%hr
%h5.mt-5 Create your account below:
= simple_form_for User.new, url: '/users', method: 'POST', html: { class:'mt-4' } do |f|
.form-group
= f.label(:phone, 'Phone')
= f.input_field(:phone, class: 'form-control', placeholder: 'Phone', required: true)
.form-group
= f.label(:first_name, 'First Name')
= f.input_field(:first_name, class: 'form-control', placeholder: 'First Name')
.form-group
= f.label(:last_name, 'Last Name')
= f.input_field(:last_name, class: 'form-control', placeholder: 'Last Name')
.form-group
= f.label(:email, 'Email')
= f.input_field(:email, class: 'form-control', placeholder: 'Enter your Email')
.form-group
= f.label(:password, 'Password')
= f.input_field(:password, class: 'form-control', placeholder: '6 characters minimum')
.form-group
= f.label(:how_about_us, 'Where did you hear about us?')
%br
= f.select :how_about_us, ['TV Commercial', 'Word of Mouth', 'Radio', 'Web', 'Letter/Postcard/Doorhanger',
'Social Media', 'Telephone Call'], class: 'form-control', prompt: 'Please Select', required: true
%hr
= hidden_field_tag(:hidden_gclid, "", :id => "00N1N00000PMKAo")
= hidden_field_tag(:current_step, 'step3')
= f.submit 'Next', class: 'btn btn-success btn-lg'
It looks like we have the form going without any errors now but we need the hidden field ID 00N1N00000PMKAo to send to salesforce but it isn't.
Since simple_form relies on the model instance you have two options here:
add a database column to the corresponding table
use a virtual attribute
Looks like you don't want to store the value in the db, in this case just add attr_accessor :hidden_gclid to your model. After it you can use it like a normal attribute inside the controller action.
How can I use simple_form to filter a field, based on a previous fields value?
For instance, I have an Opportunities form, with two fields, Company and Contact.
Company Field:
<div class="form-group">
<%= f.association :company, collection: Company.all.order(:account), prompt: "", :label_method => :account, :value_method => :id %>
</div>
Contact Field:
<div class="form-group">
<%= f.association :contact, collection: Contact.all.order(:first_name), prompt: "", :label_method => lambda { |contact| "#{contact.first_name} #{contact.last_name}" }, :value_method => :id %>
</div>
Here is what I want to do: If I select a company called "Deviant" from the Company field above, I want the Contact field to only display those contacts associated with the company called "Deviant".
I am trying something like this, but can't get it to work:
<div class="form-group">
<%= f.association :contact, collection: Contact.where("company_id = ?", params[:id]), prompt: "", :label_method => lambda { |contact| "#{contact.first_name} #{contact.last_name}" }, :value_method => :id %>
</div>
I don't know how to reference the value in the Company field.
How can I do this?
Thanks.
Update
Anyone? Surely this must be possible. This is a key functionality in any form. I would hope I don't need jQuery or something.
I think the best approach is to use ajax requests to update your contacts collection dinamically whenever the company's selected value is changed.
First you'll need an action in your contacts controller:
app/controllers/contacts_controller.rb
class ContactsController < ApplicationController
def contacts_list
if params[:company_id]
#contacts = Contact.where(company_id: params[:company_id])
else
#contacts = Contact.all
end
respond_with(#contacts) do |format|
format.json { render :json => #contacts.to_json(:only => [:id, :first_name, :last_name]) }
end
end
end
Add this to your routes:
config/routes.rb
post 'contacts_list' => "contacts#contacts_list", as: :contacts_list
Then use the coffeescript code bellow to populate your contacts' collection:
app/assets/javasctipts/companies.js.coffee
$(document).ready ->
if $("#opportunity_company_id")
populate_contacts()
$("#opportunity_company_id").change ->
populate_contacts()
populate_contacts = ->
$contacts_select = $("select#opportunity_contact_id")
$contacts_select.attr "disabled", "disabled"
company_id = $("select#opportunity_company_id").val()
if company_id is ""
$contacts_select.html "<option value=\"\">(select the company first)</option>"
else
$contacts_select.html "<option value=\"\">(loading contacts...)</option>"
data = {company_id: company_id}
data[window._auth_token_name] = window._auth_token
$.ajax "/contacts_list",
type: "post"
dataType: "json"
data: data
success: (contacts) ->
_html = '<option value="">Select the contact:</option>'
_html += '<option value="'+contact.id+'">'+contact.first_name + ' ' + contact.last_name + '</option>' for contact in contacts
$contacts_select.html _html
$contacts_select.removeAttr "disabled"
error: ->
alert 'Error trying to load contacts.'
Finally, inside your html's head tag:
<% if protect_against_forgery? %>
<script>
window._auth_token_name = "<%= request_forgery_protection_token %>";
window._auth_token = "<%= form_authenticity_token %>";
</script>
<% end %>
Hope it helps...
update:
Add the following line to your ApplicationController (app/controllers/application_controller.rb):
respond_to :html, :xml, :json, :js
I have created a database of users in my Ruby on Rails app, and now I'm trying to create a mailer that send emails to all users in my database whenever I want.
Here's my model:
class MailMessage < ActionMailer::Base
def contact(recipient, subject, message)
# host = Hobo::Controller.request_host
# app_name = Hobo::Controller.app_name || host
#subject = subject
# #body = { :user => user, :host => host, :app_name => app_name }
#body["title"] = 'This is title'
#body["email"] = 'mark#doc.org.uk'
#body["message"] = message
#recipients = recipient
#from = 'no-reply#doc.org.uk'
#sent_on = Time.now
#headers = {}
end
end
Here's my controller:
class MailMessageController < ApplicationController
def sendmail
email = #params["email"]
recipient = email["recipient"]
subject = email["subject"]
message = email["message"]
MailMessage.deliver_contact(recipient, subject, message)
return if request.xhr?
render :text => 'Message sent successfully'
end
def index
render :file => 'app/views/mail_message/index.html'
end
end
Here's my views/mail_message:
<h1>Send Email</h1>
<%= form_tag :action => 'sendmail' %>
<p>
<label for="email_subject">Subject</label>
<%= text_field 'email', 'subject' %>
</p>
<p>
<label for="email_recipient">Recipient</label>
<%= text_field 'email', 'recipient' %>
</p>
<p>
<label for="email_message">Message</label>
<%= text_area 'email', 'message' %>
</p>
<%= submit_tag "Send" %>
<%= form_tag %>
Here's my enviroment.rb:
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}
ActionMailer::Base.perform_deliveries = true # the "deliver_*" methods are available
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html" # default: "text/plain"
ActionMailer::Base.default_mime_version = "1.0"
ActionMailer::Base.default_implicit_parts_order = [ "text/html", "text/plain"]
When I run a test message, I get the following error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
app/controllers/mail_message_controller.rb:4:in `sendmail'
It doesn't seem to recognise sendmail, but I have given its location. Any clues for how to fix this error will be very appreciated.
It looks like this line is the problem:
#params["email"]
If it's meant to be the data from the form, drop the #.
#params isint initialized in your controller.
You probably simple want to use params to get your http action parameters.
I have the following problem. I have a form which takes input for a "Chart" object. But after processing the form, i wish to display one of the values, and it adds the key of this value.
Class model
class Chart
attr_accessor :title, :series
def initialize(title = nil, series = [])
#title, #series = title, series
end
end
View of form:
<% form_for :chart , :url => { :action => "show" } do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>...
<% end %>
Chart controller, show method:
def show
#chart = Chart.new(params[:chart])
end
View of show:
<h2><%=h #chart.title %></h2>
Which displays: "title"input_forms_title""
for example: writing in the input form: Economy, prints in the show view: "titleEconomy"
Any ideas?
I have just figured it out. The problem was in the constructor or initialize method. By changing the initialize method to:
def initialize( options = {} )
#title = options[:title]
#series = []
end
It now accepts all params perfectly!