RuNpiXelruN RuNpiXelruN - 4 years ago 131
Ruby Question

Rails: Is there a way of having only one contact form for a site that generates different HTML emails depending on what page it came from?

I'm building a rails app at the moment and am wondering if there is a way of simplifying the contact form process. Initially I was going to build a contact form for the home page, and a contact form for a 'become a partner' page. Each with their own controllers and html emails. However it got me wondering if there is a way of having only the one contact form and controller that generates the appropriate html email depending on which page it was submitted on? Basically I'd like the client to get an email with either, 'new message from home page' or 'new message from become a partner page'.

I'm sure there's a way but I'm having difficulty working out the relationship.


Answer Source

You can create a partial _form.html.erb and call it wherever you want.

Inside your controller you can check from where request came by calling request.original_url.

Thus your home.html.erb could be

<HTML for home page>
<%= render 'form' %>
<more HTML for home page>

Your become_a_partner.html.erb could be

<HTML for partner page>
<%= render 'form' %>
<more HTML for partner page>

Your _form.html.erb could be

<%= form_for(@contact) do |f| %>
  <%= f.text_field :nome %>
  <%= f.text_field :email %>
  <%= f.text_area :message %>
<% end %>

And finally you can check from where request came so that you can set proper message.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download