我有一个表示电子邮件的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 %>
答案 0 :(得分:0)
你的messgae不包含接收器区域。如果您希望它包含,请将其添加到数据库中。如果它只是表单和控制器代码的临时数据,则使用hidden_field_tag
而不是f.hidden_field
。后者仅适用于模型的字段。