0

I'm making a contact form in Rails 3 with this usefull example

I place my form on the main page, but when I'm trying to send message, page is redirecting to mysite.com/contact instead of mysite.com. Locally it works perfectly, but there is problem when it's deployed on Heroku. I believe that problem is in routes.rb, but I don't know how to solve it.

routes.rb

FirstApp::Application.routes.draw do
  root to: 'static_pages#home'

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

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

Thanks!

Pavel
  • 1,934
  • 3
  • 30
  • 49
  • seems that your message is not valid as it renders `:new` instead of redirecting – Sergey Kishenin Sep 13 '13 at 15:18
  • Looks like your validation is failing. Why bother checking for email format? Interesting discussion here on why it's kinda futile.. http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – muttonlamb Sep 13 '13 at 15:24

0 Answers0