Trying to create a contact form

I’ve been working on a contact form, however, most of the examples I see
posted are outdated and I’m just having some trouble getting this to
work. Here’s what I’ve done so far:

At the bottom of my environment.rb file I’ve placed:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => “mail.ncaastatpages.com”,
:port => 25,
:domain => “ncaastatpages.com
}

==========================================
In mail.yml within config I’ve placed the following:

development:
:address: mail.ncaastatpages.com
:port: 25
:authentication: plain
:user_name: [email protected]
:password: mypassword

I created a mailer model which contains the method for notifications

def notifications(sent_at = Time.now)
subject params[:subject]
recipients ‘[email protected]
from params[:address]
sent_on sent_at
body :greeting => params[:body], :sender_name => params[:name]
end

==========================================

I created the contact controller which contains:

class ContactController < ApplicationController
def index
# render index.html.erb
end

def send_mail
Mailer.deliver_notifications(params[:email])
flash[:notice] = “Email was succesfully sent.”
redirect_to :action => “index”
end
end

===========================================

I have the form in my contact view for index.html.erb

<% form_tag :action => "send_mail" do %> <%= label :email, :name, "Name" %>
<%= text_field :email, :name %> <%= label :email, :address, "Your Email Address" %>
<%= text_field :email, :address %> <%= label :email, :subject, "Subject" %>
<%= text_field :email, :subject %> <%= label :email, :body, "Your Message" %>
<%= text_area :email, :body, :rows => 8, :cols => 50 %> <%= submit_tag "Submit" %> <% end %>

==================================================

I added the following map route:

map.contact ‘/contact/send_mail’, :controller => ‘contact’, :action =>
‘send_mail’

==================================================

I go to the contacts page and see the form fine.

When I enter the information and submit I receive the following error:

undefined local variable or method `params’ for #Mailer:0x3c9d6f8

It shows contact_controller.rb:7:in `send_mail’

which contains…

Mailer.deliver_notifications(params[:email])

==================================================

I know I’m probably doing a few things wrong but mailing seems to be a
very difficult process for me to learn. Any help would be appreciated.

Thanks.

def notifications(sent_at = Time.now)
subject params[:subject]

You’re not in your controller anymore - you can’t access the params
hash like that. Anything you need in your mailer needs to be passed as
an argument to it (although you could pass params hash itself if you
wanted)

Fred

Well I fixed one thing:

map.contact ‘/contact’, :controller => ‘contact’, :action => ‘index’
map.send_mail ‘/contact’, :controller => ‘contact’, :action =>
‘send_mail’

… and now I receive no errors but I also receive no mail…

Still need help - thanks.

Frederick C. wrote:

def notifications(sent_at = Time.now)
subject params[:subject]

You’re not in your controller anymore - you can’t access the params
hash like that. Anything you need in your mailer needs to be passed as
an argument to it (although you could pass params hash itself if you
wanted)

Fred

so I need to do @subject = params[:subject] in the controller and then
supply @subject in the model?

On 21 Jun 2009, at 17:24, “Älphä Blüë” <rails-mailing-list@andreas-
s.net> wrote:

Controller =

def send_mail
@name = params[:name]
@subject = params[:subject]
@body = params[:body]
@address = params[:address]

The controller and the mailer are different objects - you can’t set
instance variables in one and expect them to appear in the other. Pass
what you need as arguments to your mailer

Fred

You can set up instance variables in your mailer model accessible
inside the view like this:

allows access to @message and @sender_name

:message => email_params[:body], :sender_name => email_params[:name]

On 21-Jun-09, at 9:18 AM, Älphä Blüë wrote:

Controller =

def send_mail
@name = params[:name]
@subject = params[:subject]
@body = params[:body]
@address = params[:address]
Mailer.deliver_notifications(params[:email])
flash[:notice] = “Email was succesfully sent.”
redirect_to :action => “index”
end

====================================================
Model =

def notifications(sent_at = Time.now)
subject @subject #params[:subject]
recipients ‘[email protected]
from @address #params[:address]
sent_on sent_at
body :greeting => @body #params[:body], :sender_name =>
params[:name]
end

====================================================
Mapping =

map.contact ‘/contact’, :controller => ‘contact’, :action => ‘index’
map.send_mail ‘/contact’, :controller => ‘contact’, :action =>
‘send_mail’

====================================================

I’m able to use the form but receive no flash messages, the form clears
out, and no mail is received (it should work in development right?)

Again, I’m new to mail so just trying to understand this all.

On Jun 21, 7:03 pm, “Älphä Blüë” [email protected]
wrote:

Again, I get no errors of any sort but no mail is received. If you see
something that I’m doing wrong, if you could provide a small code
snippet for correction, I will understand your explanation better.

At this point it’s probably down to action mailer’s configuration. You
mention you’ve setup config/mail.yml - that’s not a rails thing
(unless that’s slipped in recently). Action mailer settings are setup
in the appropriate environment file (so development.rb for the
moment).

Fred

I’m sorry if I’m newbish on this subject and some things I understand
and others I don’t. I believe I have this very close to being correct:

My Mailer modle is mailer.rb

class Mailer < ActionMailer::Base

def notifications(email_params, sent_at = Time.now)
subject email_params[:subject]
recipients ‘[email protected]
from email_params[:address]
sent_on sent_at
body :greeting => email_params[:body], :sender_name =>
email_params[:name]
end

end

As you can see it has the class Mailer which is a part of
ActionMailer::Base. I’m also defining the method for notifications and
asking for email_params to be sent to it.

The controller for my contacts page:

def send_mail
Mailer.deliver_notifications(params[:email])
flash[:notice] = “Email was succesfully sent.”
redirect_to :action => “index”
end

Contains the method for send_mail which is making a direct call to the
Mailer class and providing params[:email].

params[:email] parameters are specified from the view:

                <% form_tag :action => "send_mail" do %>
                  <tr><td>
                      <%= label :email, :name, "Name" %><br />
                      <%= text_field :email, :name %>
                  </td></tr>
                  <tr><td>
                      <%= label :email, :address, "Your Email 

Address" %>

<%= text_field :email, :address %>


<%= label :email, :subject, “Subject” %>

<%= text_field :email, :subject %>


<%= label :email, :body, “Your Message” %>

<%= text_area :email, :body, :rows => 8, :cols
=> 50 %>


<%= submit_tag “Submit” %>

<% end %>

Again, I get no errors of any sort but no mail is received. If you see
something that I’m doing wrong, if you could provide a small code
snippet for correction, I will understand your explanation better.

Thanks for the input Fred.

I actually removed that - it was being “potentially” used for something
else down the road, so it really didn’t apply here.

I’ve decided to take an entirely different approach with the contact
form at this point. I believe the easier way to implement it, and even
administrate it, would be to use a scaffold with restful and then remove
the layout file. I then have everything in place. I was looking at the
following video:

http://s3.amazonaws.com/lr_screencasts/learningrails-18.mov

It was a fairly good watch and I liked how they did the approach. I
know there is probably a simpler way for implementing this but with a
scaffold approach, it will also make testing easier.

Thanks mate - wish me luck.