我有两种模式:
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
?我显然做错了什么,但我无法弄清楚是什么......
感谢您的帮助