lundi 16 mai 2016

Rails form not causing errors

I've been working on a rails form and think I've messed something up, but cant put my finger on what.

To give an overview. I have created a 1 page site with a contact form at the bottom. I have looked around and similar questions and they seem to have the contact form on another page with another controller, so can't find an answer for me. I know i've missed something, but can't figure out what.

I get an error which states

First argument in form cannot contain nil or be empty

My main_controller.rb is as follows

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

def create
 @contact = Contact.new(contact_params)
 @contact.request = request
if @contact.deliver
  redirect_to thank_you_path
else
  flash.now[:error] = 'Cannot send message.'
  render :index
 end
end

 private
  def contact_params
   params.require(:contact).permit(:name, :email, :phone, :message)
  end
end

contact.rb is

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

def headers
{
  subject: "Quote Form",
  to: "jtbates.iphone@gmail.com",
  from: %("#{name}" <#{email}>)
}
end
 end

index.html.erb

<%= bootstrap_form_for(@contact, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| %>
 form template here
<% end %>

routes/rb

Rails.application.routes.draw do
 resources :main, only: [:new, :create]
 root 'main#index'
 get '/thank-you'          => 'main#thank-you'
end

Aucun commentaire:

Enregistrer un commentaire