消息中缺少消息线程的id(accepts_nested_attributes_for)

时间:2012-09-26 13:06:15

标签: ruby-on-rails-3 nested-attributes

我有两种模式:

class MessageThread < ActiveRecord::Base
    has_many :messages
    attr_accessible :messages_attributes, :subject
    accepts_nested_attributes_for :messages
end

class Message < ActiveRecord::Base
    belongs_to :message_thread
    belongs_to :sender, :polymorphic => true
    attr_accessible :sender_id, :sender_type, :sender, :content
end

在我的new.html.erb视图中,事情看起来像这样:

<%= simple_form_for @message_thread do |f| %>
    <%= f.input :subject, :label => t('message.subject')%>
    <%= f.simple_fields_for :messages do |m| %>
        <%= m.input :content , :label => t('message.content') %>
    <% end %>

    <%= f.button :submit, t('message.send_message') %>
<% end %>

在我的MessageThreadsController内容看起来像这样:

def new
   @message_thread = MessageThread.new
   @message_thread.messages.build
end

def create
   @message_thread = MessageThread.new(params[:message_thread])
   @message_thread.save!
end

我得到的错误是Message thread can't be blank。如果在保存message_thread_id之前我不知道它会是什么,我怎么设置这个@message_thread?我显然做错了什么,但我无法弄清楚是什么......

感谢您的帮助

0 个答案:

没有答案