Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!-- app/views/inquiries/_form.html.erb
<%= form_for inquiry do |f| %>
<% if inquiry.errors.any? %>
<ul>
<% inquiry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div style="display: none;">
<%= f.label :nickname %><br/>
<%= f.text_field :nickname %>
</div>
<div>
<%= f.label :name %><br/>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</div>
<div>
<%= f.label :message %><br/>
<%= f.text_area :message %>
</div>
<div>
<%= f.submit "Send" %>
</div>
<% end %>
# app/models/inquiry.rb
class Inquiry
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActionView::Helpers::TextHelper
attr_accessor :name, :email, :message, :nickname
validates :name,
:presence => true
validates :email,
:format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :message,
:length => { :minimum => 10, :maximum => 1000 }
validates :nickname,
:format => { :with => /^$/ }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def deliver
return false unless valid?
Pony.mail({
:from => %("#{name}" <#{email}>),
:reply_to => email,
:subject => "Website inquiry",
:body => message,
:html_body => simple_format(message)
})
end
def persisted?
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment