Tom Pinchen
Tom Pinchen

Reputation: 2497

Contact Form Mailer Undefined Method Error Rails

I have been having some serious trouble all weekend trying to get this basic contact form to work correctly.

Effectively I want the user to be able to complete the form, hit send and have the message send straight to a predefined email address.

The error I am continually getting is:

  NoMethodError in Messages#new

  Showing C:/Sites/jobapp_v2/app/views/messages/new.html.erb where line #1 raised:

   undefined method `[]' for nil:NilClass

  Extracted source (around line #1):

  1: <%= form_for @message, :url => contact_path do |f| %>
  2: 
  3:    <div class="field">
  4:      <%= f.label :name %>

I have the following setup:

Messages controller

class MessagesController < ApplicationController

def new
   @user = current_user
    @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

Message Model

 class Message < ActiveRecord::Base

   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

Routes.rb

 JobappV2::Application.routes.draw do
   devise_for :users

   resources :newsletters

   match "contact" => "messages#new", :as => "contact", :via => :get
   match "contact" => "messages#create", :as => "contact", :via => :post

   get "pages/about"

   get "pages/contact"

   get "pages/terms"

   resources :jobs

   resources :users do
   resources :jobs
   end


   root :to => 'jobs#index'


   end

NotificationsMailer.rb

 class NotificationsMailer < ActionMailer::Base

   default :from => "[email protected]"
   default :to => "[email protected]"

   def new_message(message)
     @message = message
     mail(:subject => "Hello")
   end
 end

Views/notification_mailer/new_message.text.erb

 Name: <%= @message.name %>

 Email: <%= @message.email %>

 Subject: <%= @message.subject %>

 Body: <%= @message.body %>

Views/messages/new.html.erb

  <%= form_for @message, :url => contact_path do |f| %>

<div class="field">
  <%= f.label :name %>
  <%= f.text_field :name %>
</div>

<div class="field">
  <%= f.label :email %>
  <%= f.text_field :email %>
</div>

<div class="field">
  <%= f.label :subject %>
  <%= f.text_field :subject %>
</div>

<div class="field">
  <%= f.label :body %>
  <%= f.text_area :body %>
</div>


<%= f.submit "Send" %>

   <% end %>

I really for can't work out at all what is causing this undefined method error so any help people can offer would really be much appreciated! Thanks in Advance :)

Upvotes: 0

Views: 397

Answers (1)

manishval
manishval

Reputation: 89

The way you have hooked things up, you don't need :url => contact_path

<%= form_for @message do |f| %>

will work just fine.

Upvotes: 1

Related Questions