Skip to content

Instantly share code, notes, and snippets.

@aalinat
Created July 14, 2012 22:43
Show Gist options
  • Save aalinat/3113728 to your computer and use it in GitHub Desktop.
Save aalinat/3113728 to your computer and use it in GitHub Desktop.
rails upload attachment (contact form)
<h1>Contact</h1>
<%= form_tag("/send_to", :method=>'post', :multipart => true) do %>
<p>
Subject:<br />
<%= text_field_tag "subject" %>
</p>
<p>
Priority: <br />
<%= select_tag("priority", options_for_select([['Critical', '1'],
['Important', '2'],['Standard','3']], '3')) %>
</p>
<p>
Describe your problem:<br />
<%= text_area_tag "description", "", :size=>"10x10" %>
</p>
<p>
Add an attachment:<br />
<%= file_field_tag "attachment" %>
</p>
<p>
<%= submit_tag 'Submit', :class => 'btn btn-primary' %>
</p>
<% end %>
New Message!
<%=@description%>
class StaticsController < ApplicationController
def contact
end
def send_to
subject = params[:subject]
description = params[:description]
attachment = params[:attachment]
Testmailer.do_contact(subject,description,attachment).deliver
flash[:notice] = "email sent"
redirect_to :back
end
end
class Testmailer < ActionMailer::Base
default :from => "[email protected]"
def do_contact(subject,description,attachment)
@description =description
if attachment
attachments[attachment.original_filename] = File.open(attachment.path, 'rb'){|f| f.read}
end
mail(:to => "[email protected]", :subject => subject)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment