我正在使用这个有用的示例在 Rails 3 中制作联系表格
我将表单放在主页上,但是当我尝试发送消息时,页面重定向到 mysite.com/contact 而不是 mysite.com。在本地它运行良好,但在 Heroku 上部署时出现问题。我相信问题出在 routes.rb 中,但我不知道如何解决。
路线.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
联系人控制器.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
消息.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
谢谢!