0
我有一個ActiveModel類,它表示和發送電子郵件消息,以及一個表單提交帶有useremail隱藏字段的消息。當我提交我的表格時,我得到一個使用activemodel和form_for的未定義方法
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\-][email protected](?:[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 %>