-
-
Save wrburgess/1337891 to your computer and use it in GitHub Desktop.
| # Majority of this solution was written and posted here: | |
| # http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3 | |
| # http://railscasts.com/episodes/206-action-mailer-in-rails-3 | |
| # config/initializers/smtp_settings.rb | |
| ActionMailer::Base.smtp_settings = { | |
| :address => "smtp.gmail.com", | |
| :port => 587, | |
| :domain => "example.com", | |
| :user_name => "[email protected]", | |
| :password => "secret", | |
| :authentication => "plain", | |
| :enable_starttls_auto => true | |
| } | |
| #create a message class in file app/models/message.rb | |
| class Message | |
| include ActiveModel::Validations | |
| include ActiveModel::Conversion | |
| extend ActiveModel::Naming | |
| attr_accessor :name, :email, :subject, :body | |
| validates :name, :email, :subject, :body, :presence => true | |
| validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true | |
| def initialize(attributes = {}) | |
| attributes.each do |name, value| | |
| send("#{name}=", value) | |
| end | |
| end | |
| def persisted? | |
| false | |
| end | |
| end | |
| # run rails g mailer ContactMailer | |
| # creates file called app/mailers/contact_mailer.rb | |
| class ContactMailer< ActionMailer::Base | |
| default :from => "[email protected]" | |
| default :to => "[email protected]" | |
| def new_message(message) | |
| @message = message | |
| mail(:subject => "#{message.subject}") | |
| end | |
| end | |
| # create the file app/views/notifications_mailer/new_message.text.erb | |
| Name: <%= @message.name %> | |
| Email: <%= @message.email %> | |
| Subject: <%= @message.subject %> | |
| Body: <%= @message.body %> | |
| # Run rails g controller contact and then open app/controllers/contact_controller.rb | |
| class ContactController < ApplicationController | |
| def new | |
| @message = Message.new | |
| end | |
| def create | |
| @message = Message.new(params[:message]) | |
| if @message.valid? | |
| NotificationsMailer.new_message(@message).deliver | |
| redirect_to(root_path, :notice => "Message was successfully sent.") | |
| else | |
| flash.now.alert = "Please fill all fields." | |
| render :new | |
| end | |
| end | |
| end | |
| # To get these actions working, open up config/routes.rb and insert the following two lines | |
| match 'contact' => 'contact#new', :as => 'contact', :via => :get | |
| match 'contact' => 'contact#create', :as => 'contact', :via => :post | |
| # create app/views/contact/new.html.erb | |
| <%= form_for @message, :url => contact_path do |form| %> | |
| <fieldset class="fields"> | |
| <div class="field"> | |
| <%= form.label :name %> | |
| <%= form.text_field :name %> | |
| </div> | |
| <div class="field"> | |
| <%= form.label :email %> | |
| <%= form.text_field :email %> | |
| </div> | |
| <div class="field"> | |
| <%= form.label :subject %> | |
| <%= form.text_field :subject %> | |
| </div> | |
| <div class="field"> | |
| <%= form.label :body %> | |
| <%= form.text_area :body %> | |
| </div> | |
| </fieldset> | |
| <fieldset class="actions"> | |
| <%= form.submit "Send" %> | |
| </fieldset> | |
| <% end %> |
Works, i move smtp_settings.rb content to environments/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'mail.mydomain.cc',
:port => 587,
:domain => 'mydomain.cc',
:user_name => '[email protected]',
:password => 'mypassword',
:authentication => :login,
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
:enable_starttls_auto => false
}
config.action_mailer.default_url_options = {
:host => "localhost:3000"
}
In the contact/new.html.erb file add :method => :post like this:
<%= form_for @message, :url => contact_path, :method => :post do |form| %>
Thank you for you tutorial, i will make a spanish version. Buen dia!
Problem with the routes
Invalid route name, already in use: 'contact' (ArgumentError)
You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming.
Thanks for this!
define your routes like this:
get 'contact' => 'contact#new'
post 'contact' => 'contact#create'

Randy, i follow your original post http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/ to here.
Im implement the steps in a localhost project. I render the notifications_mailer/new_message.text.erb in the terminal, but my question is: if only work in production or in a hosting project.
I hope you can anwser me. Thank you!