【发布时间】:2011-01-29 06:00:48
【问题描述】:
我有一个Topic,其中有很多Posts。创建主题时,它会随之创建第一个帖子。
我在表单中包含了帖子字段:
= form_for @topic do |topic_form|
# ...
= topic_form.fields_for @post do |post_fields|
= post_fields.label :content
%br/
= post_fields.text_area :content
%br/
这是我的TopicsController 的样子:
def new
@topic = Topic.new
@post = Post.new
respond_with @topic
end
def create
@topic = Topic.create params[:topic]
@post = @topic.create_post params[:topic][:post]
respond_with @topic, location: topic_url(@topic)
end
我在create 方法的第一行得到一个UnknownAttributeError - unknown attribute: post。我猜这是因为帖子哈希包含在请求的主题哈希中:
"topic" => { "title" => "...", "post" => { "content" => "..." } }
我该如何解决这种情况?
【问题讨论】:
标签: ruby-on-rails forms associations has-many