我正在使用这个有用的example
在Rails 3中创建一个联系表单我将表单放在主页面上,但是当我尝试发送消息时,页面将重定向到mysite.com/contact而不是mysite.com。在本地它可以很好地工作,但是当它部署在Heroku上时会出现问题。我相信问题出在routes.rb中,但我不知道如何解决它。
的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
谢谢!