我有一个表示电子邮件消息的 ActiveModel 类和一个提交带有隐藏字段的消息的表单,用于 useremail。当我提交表单时,我得到一个
undefined method `receiver=' for <Message:0x007fce44d495f8>
.
class Message
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
include ActionView::Helpers::TextHelper
attr_accessor :name, :email, :contenu
validates :name,
:presence => true
validates :email,
:format => { :with => /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/ }
validates :content,
:length => { :minimum => 10, :maximum => 1000 }
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
我的表格:
<%= form_for @message, :url => {:action => "contact"}, :method => "post" do |f| %>
<% @message.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<%= f.hidden_field :receiver, :value => @listing.user.email %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :contenu, "Message" %><br />
<%= f.text_area :contenu %>
</p>
<p><%= f.submit "Send Message" %></p>
<% end %>