我有一些未发布的表单存在问题,并且日志中没有显示任何内容。我不确定问题是来自activemodel,文件名约定还是路由。没有消息被发送。
我的控制器非常基本,它位于名为* promessages_controller.rb *的文件中
# coding: utf-8
class PromessagesController < ApplicationController
def new
@promessage = Promessage.new
end
def create
@promessage = Promessage.new(params[:promessage])
if @promessage.valid?
PromessageMailer.contact_us(@promessage).deliver
redirect_to(root_path, :notice => "Sent.")
else
redirect_to(root_path, :notice => "Error")
end
end
end
我的邮件程序位于名为* promessage_mailer.rb *
的文件中class PromessageMailer < ActionMailer::Base
def contact_us(message)
@message = message
mail(:to => 'xxx@gmail.com', :subject => "test", :from => "info@mydomain.com")
end
end
我的activemodel模型位于名为 promessage.rb 的文件中,包含以下内容:
class Promessage
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :email, :name, :body
validates :name, :email, :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
我的表单位于new.html.erb中,位于views / promessages
下<%= simple_form_for @promessage do |f| %>
<% @promessage.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<%= f.input :email, label: 'email' %>
<%= f.input :body, label: 'text', as: :text, :input_html => { :cols => 50, :rows => 5 } %>
<%= f.submit "Send", :class => 'btn btn-primary' %>
<% end %>
我在views / promessage_mailer
中有我的文本和html模板在我的路线中我添加了:
resources :promessages, only: [:new, :create]
每次我提交表单时,它都会在控制器中显示错误,甚至不验证表单。我错过了什么?