0

我有一个表示电子邮件消息的 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 %>
4

1 回答 1

0

您的消息不包含接收者字段。如果您希望它包含,则将其添加到数据库中。如果只是表单和控制器代码的临时数据,则hidden_field_tag使用f.hidden_field. 后者仅适用于模型的字段。

于 2013-06-02T20:27:52.897 回答