我有两个控制器页面和联系人。页面显示数据库中的内容。联系是简单的联系表格,效果很好。现在我想将此联系表放在特定页面中。但我尝试渲染它,但仍然出现没有模型类等错误。
这是我的联系人控制器(控制器/contact_controller):
class ContactController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:message])
if @message.valid?
NotificationMailer.new_message(@message).deliver
redirect_to(root_path, :notice => "Sent.")
else
flash.now.alert = "Alert."
render :new
end
end
end
这是我的消息模型(models/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
这是表格(views/contact/new.html.erb):
<%= form_for @message, :url => contact_path do |form| %>
<fieldset class="fields">
<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>
<div class="field">
<%= form.label :subject %>
<%= form.text_field :subject %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
</fieldset>
<fieldset class="actions">
<%= form.submit "Send" %>
</fieldset>
<% end %>
如何将此表单放入页面视图???