我有两个模型:
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
?我显然做错了什么,但我不知道是什么......
谢谢你的帮助