Params: hash

Hi.

I'm really struggling to get a simple contact from to work.

In my controller I have:

def index     @contact = Contact.new(params[:contact])     Contact::deliver_signup_thanks(@contact)   end

I want to pass @contact into 'deliver_signup_thanks' so it can reference each item via 'contact.name, contact.email' etc

Can anyone tell me how you do this?

Try creating another class to handle your emails, it should extend ActionMailer. As an example:

Create models/account_notifier.rb

class AccountNotifier < ActionMailer::Base   def signup_thanks(contact)     ...     do whatever you want with the information     ...   end end

Call it like this: AccountNotifier.deliver_signup_thanks(@contact)

Creat the following in views: views/account_notifier/signup_thanks.rhtml

Does that help?

Hi Naffis

Sorry if I am being very stupid here, have just started learning Ruby.

What I am after knowing is - say I have @contact = params[:contact] this has has all the values of the submitted form. How do I the get this into - I think the terminology is an object. I basically want to be able to do @contactobj.name, @contactobj.email etc to get the values out.

The reason for this is rather than passing say email, subject etc all in sperately to the send email function I want to pass them in one go.

Jono,

Not stupid, I misunderstood your question. You were on the right track. You should do something like:

@contact = Contact.new(params[:contact])

That will populate the Contact object. If you're having problems then I suspect it's because you're not setting up your form properly. The params get passed as contact[name] and contact[address] for example. Following this same example, in your html form you need to have something like:

<%= text_field("contact", "name") %> and <%= text_field("contact", "address" %>

This will output the html in the format: <input type="text" name="contact[name]">

Any better?

Hi Naffis

Thanks again for your help. I'm pretty sure the form is setup correctly, they are in format like your example.

The following errors:

class ContactController < ApplicationController   layout "main"

  def index     @contact = Contact.new(params[:contact])    render :text => @contact.name   end

end

Should I expect this to work?

Yeah that should do it.

The following errors:

class ContactController < ApplicationController   layout "main"

  def index     @contact = Contact.new(params[:contact])    render :text => @contact.name   end

end

Should I expect this to work?

Yes. If it doesn't, do a p params as the first statement in the controlle method and have post the output here.

Max

Hi Max

This is the ouput.

Processing ContactController#index (for 127.0.0.1 at 2006-08-29 08:21:39) [POST]   Session ID: 9cb6b17f2facd4babb4042a50104b004   Parameters: {"commit"=>"Send", "contact"=>{"name"=>"Jono", "area_of_interest"=>"General", "telephone_number"=>"12345656", "email"=>"jono.brain@gmail.com"}, "action"=>"index", "controller"=>"contact"}

When I try and do @contact.name I get the following error

You have a nil object when you didn't expect it! The error occured while evaluating nil.name

That's quite bizarre. The parameters are where they should be, and in any case even if they were not, this would not lead to a nil object.

@contact = Contact.new(...) should *always* result in @contact being something other than nil.

Can you paste your controller code verbatim please? The only reason I currently see is a typo somewhere.

Cheers, Max

Hi Max

Code is at home, will paste it in when I get back from work today. Thankyou so much for your help.

Hi max

This is the code:

-- controllers/contact_controller.rb --

class ContactController < ApplicationController   layout "main"

  def index     p params     if request.post?      @contact = Contact.new(params[:contact])     # Contact::deliver_signup_thanks(@contact)     render :text => @contact.name     end   end

end

-- views/contact/index.rhtml ---

<% @page_title = "contact" %> <% @page_class = "contact" %> <div id="ContentPrimary">   <h1><span>contact us</span></h1>   <div id="form">     <% form_for :contact do |form| %>     <h2>lorem ipsum dolor sit amet</h2     <h3>Donec fringilla. Nam odio ipsum, pretium tincidunt, eges tas ut, hendrerit in, nisl. Suspendisse sagittis sceler isque ipsum</h3>     <%= error_messages_for 'contact' %>

    <div class="row input">       <label for="contact_name">Your name:</label>       <%= form.text_field :name %></div>

    <div class="row input">       <label for="contact_telephone_number">Your telephone number:</label>       <%= form.text_field :telephone_number %></div>

    <div class="row input">       <label for="contact_email">Your email address:</label>       <%= form.text_field :email %></div>

    <div class="row select">       <label for="contact_area_of_interest">Area of interest:</label>       <%= select("contact", "area_of_interest", Portfolio.find_all.collect {|p| [ p.name, p.name ] } ) %></div>

    <%= submit_tag "Send" %>     <% end %>   </div>   <div id="address">     <h3>adrian bodie</h3>     <p>Stanley Wharf Mill<br />Kirkebrok Rd<br />Deane<br />Bolton<br />BL3 3JE</p>     <div class="telephone">       <h4>telephone</h4>       <p>01294 665 200</p>     </div>     <div class="fax">       <h4>fax</h4>       <p>01294 665 200</p>     </div>     <div class="email">       <h4>email</h4>       <p>info@adrianbodie.com</p>     </div>   </div>   <div class="cleaner"></div> </div>

-- models/contact.rb

class Contact < ActionMailer::Base

  def signup_thanks( contact )     @body["contact"] = contact

    # Email header info MUST be added here     @recipients = contact.email

    # Email body substitutions go here

  end

end

-- ERROR I GET --

NoMethodError in ContactController#index

You have a nil object when you didn't expect it! The error occured while evaluating nil.name RAILS_ROOT: /Users/jono/Sites/adrianbodie/public/../config/..

Application Trace | Framework Trace | Full Trace #{RAILS_ROOT}/app/controllers/contact_controller.rb:9:in `index' Request

Parameters: {"commit"=>"Send", "contact"=>{"name"=>"cds", "area_of_interest"=>"General", "telephone_number"=>"xcacsc", "email"=>""}}

Show session dump

Response

Headers: {"cookie"=>, "Cache-Control"=>"no-cache"}

Aha!

-- models/contact.rb

class Contact < ActionMailer::Base                               ^^^^^^^^^^^^^^^^

There's your problem right there. Contact should inherit from ActiveRecord if you want to use the Contact.new(hash) method.

Semantically, a contact isw a domain model object, not a Mailer.

I suggest creating a Contact model that inherits from AR (or, if you don't want to store contacts in the DB, use the active-form plugin) and a ContactNotifier class which inherits from ActionMailer.

Then in your controller do

... @contact = Contact.new(params[:contact]) ContactNotifier.deliver_signup_thanks(@contact) ...

That should do the trick.

Cheers, Max

Excellent. Thankyou so much Max, much appreciated it. It was driving me crazy.

Hi Max,

May I ask you to elaborate on this? I recently created an object called query and managed to get it so that I can use the line:

  @query = Query.new(params[:query])

by picking it up like this (roughly):

def initialize attributes = nil   unless attributes.nil?     @attributes = attributes     @name = @attributes[:name]     etc..   end end

I don't want to store queries in the database so Query doesn't inherit from ActiveRecord but despite the fact that I have used params (AND IT WORKS) I don't fully understand what it does. Does your post imply that there is a better way to do it than the way I have done it? (with an active_form plugin?) Can you explain further? Karen

active_form gives you a new base class for your models which behaves exactly like an ActiveRecord, which means you can use validations easily. Your ActiveForm objects become drop-in replacements for any of your ActiveRecord classes.

The cases where this is useful are a) you don't need to store the model content in the database, but you still want to use validations and scaffolds; and b) your form does not map 1-to-1 to your model, for example createing more than one model object out of a single form.

Do a google search on validating non-activerecord forms, that shoiuld turn up a few more pointers.

Cheers, Max