Contact form - NoMethodError (undefined method `each' for nil:NilClass)

Hi Guys,

So basically I want a contact form to appear on all the service pages. I’ve already created a contact form for the contact page which works perfectly using this tutorial: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/

Now I would like to implement this approach on the service pages, and it works until I push the Submit button, when I get the following error:

Processing by ScontactController#create as HTML

Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method each' for nil:NilClass): app/models/servicemail.rb:13:in initialize’ app/controllers/scontact_controller.rb:9:in new' app/controllers/scontact_controller.rb:9:in create’

The parameters are ok.

Rails seems to complain about the each method in the model: class Servicemail

include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming

attr_accessor :name, :email, :subject, :body, :szolg

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

This is the controller: class ScontactController < ApplicationController

def new @message = Servicemail.new @szolg = Service.find_by_permalink!(params[:id]) end

def create @message = Servicemail.new(params[:message]) @szolg = Service.find_by_permalink(params[:id])

if @message.valid?
  ServiceContact.new_message(@message).deliver
  redirect_to(szolg_path(@szolg), :notice => "Message sent successfully!")
else
  flash.now.alert = "Please fill out all the required fields!"
  render :new
end

end

end

Is it the name of the model/view/controller that bothers Rails or is it something else?

Best Wishes, Matt

Hi Guys,

So basically I want a contact form to appear on all the service pages. I've already created a contact form for the contact page which works perfectly using this tutorial: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/

Now I would like to implement this approach on the service pages, and it works until I push the Submit button, when I get the following error:

Processing by ScontactController#create as HTML

Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `each' for nil:NilClass):   app/models/servicemail.rb:13:in `initialize'

That means that at that line of code you are calling 'each' on something that is nil.

  app/controllers/scontact_controller.rb:9:in `new'   app/controllers/scontact_controller.rb:9:in `create'

The parameters are ok.

Rails seems to complain about the each method in the model: class Servicemail

  include ActiveModel::Validations   include ActiveModel::Conversion   extend ActiveModel::Naming

  attr_accessor :name, :email, :subject, :body, :szolg

  validates :name, :email, :subject, :body, :presence => true   validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

  def initialize(attributes = {})     attributes.each do |name, value|

Assuming that the above is line 13 then it means that attributes is nil.

Colin