Lee Graham
Lee Graham

Reputation: 241

Contact form in Rails

This is a pretty basic question, but I'm having difficulty incorporating it in to my application. I've looked at other similar questions on SO, but they all are using the contact page as a separate page.

I have a front-end of a Rails app that is just a small public facing site using stellar.js for parallax scrolling.

At the last part of the page, I would like to have a "Contact Us" form, but I'm getting a

NoMethodError in Welcome#index undefined method `model_name' for NilClass:Class

Here are the related files:

routes.rb

TestApp::Application.routes.draw do
   get "welcome/index"
   root :to => 'welcome#index'
   match 'contact' => 'contact#new', :as => 'contact', :via => :get
   match 'contact' => 'contact#create', :as => 'contact', :via => :post

app/views/welcome/index.html.erb

<%= render "contact" %>

The partial for the contact section of the page app/views/welcome/_contact.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 %>

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

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

The question is what do I need to do to make this form to function correctly in the welcome#index view?

Upvotes: 0

Views: 927

Answers (2)

Rahul Tapali
Rahul Tapali

Reputation: 10137

You can change following things:

In routes:

Remove:

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

Add:

resources :contact   # It will add 7 default REST routes and standard practice model name should be plural if you created your model with singular then keep it singular 

In _contact.html.erb partial form path needs to be new_contact_path

<%= form_for Message.new, :url => contact_path do |form| %>

class Message should inherit ActiveRecord::Base

class Message < ActiveRecord::Base

end 

In create action else part should be:

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 :template => "welcome/index"
end 

Upvotes: 1

Chris Peters
Chris Peters

Reputation: 18090

You need to have @message = Message.new defined in every action that needs @message to be present.

So that means you need this in WelcomeController:

def index
  @message = Message.new
end

A shortcut is to just add it to the top of the partial in case it's not set:

<% @message ||= Message.new %>

(But you need to decide if mixing in calls to the model within your views is your style. Some devs don't have a problem with this, and some do.)

Upvotes: 2

Related Questions