You likely need to build the Bar
object under @foo
. ie
def new
@foo = Foo.new
@foo.bars.build
end
def create
@foo = Foo.new(params[:foo])
@foo.bars.first.author = current_user
if @foo.save
redirect_to @foo
else
render action: "new"
end
end
A good resource for nested forms: http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast
Provided you set accepts_nested_attributes_for in your model and correctly used fields_for
in your form, params[:foo]
will contain an element called bar_attributes
that will contain the data entered on the form about the bar object. Look through the railcast I linked to for more info.