Skip to content

Instantly share code, notes, and snippets.

@paulhuisman
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulhuisman/05005f4d352cbdc157f6 to your computer and use it in GitHub Desktop.
Save paulhuisman/05005f4d352cbdc157f6 to your computer and use it in GitHub Desktop.
Simpleform Mailform Rails
# model; quotations.rb
class Quotation < MailForm::Base
attribute :name, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :organisation, :validate => true
attribute :telephone, :validate => true
attribute :attachment, :attachment => true
attribute :message
# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "Awesome mail - new quotation request",
:to => 'paul@test.com',
:from => %("#{name}" <#{email}>)
}
end
end
# controller; quotations_controller.rb
class QuotationsController < ApplicationController
skip_before_filter :add_current_store_id_to_params
def new
@quotation = Quotation.new
end
def create
@quotation = Quotation.new(params[:quotation])
@quotation.request = request
if @quotation.deliver
flash[:notice] = Spree.t(:quotation_thank_you_message)
puts 'Send quotation mail succesfull!'
redirect_to('/products/' + params[:quotation]['product_id'])
else
flash[:error] = 'Cannot send message. Please contact the site administrator.'
puts 'Send quotation mail NOT succesfull'
redirect_to('/products/' + params[:quotation]['product_id'])
end
end
end
# form template
<h2 class="title-big"><%= Spree.t(:request_a_quotation) %></h2>
<%= simple_form_for @quotation, :url => '/quotations', :html => {:class => 'form-horizontal' } do |f| %>
<%= f.input :name, :required => true, :label => Spree.t('name') %>
<%= f.input :organisation, :required => true, :label => Spree.t('organisation') %>
<%= f.input :email, :required => true, :label => Spree.t('email') %>
<%= f.input :telephone, :required => true, :label => Spree.t('telephone') %>
<%= f.input :message, :as => :text, :label => Spree.t('message'), :required => true, :input_html => { :cols => 5, :rows => 10 } %>
<%= f.input :attachment, :as => :file, :label => Spree.t('attachment') %>
<%= f.input :product_id, :as => :hidden, :input_html => { :value => @product.id } %>
<div>
<%= f.button :submit, Spree.t('send_quotation'), :class=> "button primary" %>
</div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment