ruby-on-railsformsmail-form

Error message when using Rails MailForm Gem - undefined method 'contacts_path


We are attempting to use the Rails MailForm Gem to generate a Contact Form for each instance of campsite within our application, so that when the form is completed by a user an email is sent straight from the form to the camp site owner.

We have nested the resource for contacts within the sites resource in routes.rb - to ensure the form sits at the path - localhost:3000/sites/1/contacts/new as opposed to localhost:3000/contacts/new.

Is there something key we are missing in our application or should we approach this problem in a different way? Advice appreciated.

Our code for this is

resources :campsites do resources :contacts end

However we receive an error when trying to view this path. The reported error states undefined method "contacts_path' for #<#<Class:0x007ff0ed9527d0>:0x007ff0ed950de0> and we are not sure why.

We've been following this walk-through (https://rubyonrailshelp.wordpress.com/2014/01/08/rails-4-simple-form-and-mail-form-to-make-contact-form/).

We've reviewed the gem and Rails documentation, but am still not able to view the form after a number of attempts, and we are not sure if it is to do with where we are trying to view the form within the app.

When running localhost:3000/sites/1/contacts/new, we receive the following error:

NoMethodError in Contacts#new
Showing /Users/elinnet/makers/week11/campFri/app/views/contacts/new.html.erb where line #4 raised:

undefined method 'contacts_path' for #<#<Class:0x007ff0ed9527d0>:0x007ff0ed950de0>
Extracted source (around line #4):

(1) <h3>Send A message to Us</h3>

(2) <%= simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f| %>
(3) <%= f.input :name, :required => true %>
(4) <%= f.input :email, :required => true %>
(5) <%= f.input :message, :as => :text, :required => true %>

The controller for contacts is:

class ContactsController < ApplicationController
    def new
     @contact = Contact.new
    end

    def create
     @contact = Contact.new(params[:contact])
     @contact.request = request
     if @contact.deliver
       flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
     else
       flash.now[:error] = 'Cannot send message.'
       render :new
     end
   end
end

The Model for contacts is:

class Contact < MailForm::Base
   attribute :name, :validate => true
   attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
   attribute :message
   attribute :nickname, :captcha => true


  def headers
  {
  :subject => "My Contact Form",
  :to => "your_email@example.org",
  :from => %("#{name}" <#{email}>)
  }
  end
end

The project Github reference is: https://github.com/elinnet/camping.git


Solution

  • You routes are

    resources :campsites do 
      resources :contacts 
    end
    

    So, you have to pass two params in an array to your form like this:

    <%= simple_form_for [@campsite, @contact], :html => {:class => 'form-horizontal' } do |f| %>
    

    And you new action in controller should look something like:

    def new
     @campsite = Campsite.find(params[:campsite_id]
     @contact = @campsite.contacts.build
    end