My model (survey, there are many questions):
class Survey
include Mongoid::Document
field :name
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :allow_destroy => true
end
class Question
include Mongoid:: Document
field :content
belongs_to :survey
end
and survey controller:
def new
@survey = Survey.new
3.times {@survey.questions.build}
....
and an opinion:
< /p>
<%= form_for(@survey) do |f| %>
<%= f.fields_for :questions do |builder| %>
<%= builder.label :content , "Question" %>
<%= builder.text_area :content, :rows => 3 %>
<%= builder.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Question" %>
<% end %>
...
In my log, I have:< /p>
Started POST "/surveys" for 127.0.0.1 at 2011-05-24 13:26:51 +0400
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"G£ ô", "authenticity_token"=>"tX0FfMiLbh1BwjuY4CuvAKt2UpTraY3vmdo58ocBnos=", "survey"=>{"name"=>"
Rails", "questions_attributes"=>{"0"=>{"content"=>" Are you fond of Rails?", "_destroy"=>"0"}, "1"=>{"content"=>"Rails is
the best, ha?", "_destroy"=>"0 "}, "2"=>{"content"=>"How many railscasts have you watched?", "_destroy"=>"0"}}}, "commit
"=>"Create Survey"}
MONGODB nested_attributes_development['surveys'].insert([{"name"=>"Rails", "_id"=>BSON::ObjectId('4ddb79dba5372914380000
69')}])
Redirected to http://localhost:3000/surveys/4ddb79dba537291438000069
MONGODB
nested_attributes_development['surveys'].insert([{"name"=>"Rails" ,
"_id"=>BSON::ObjectId('4ddb79dba5372914380000
69')}])
This will not be saved to the unfinished question collection
mongoid default.
Just add has_many:questions,:dependent =>:destroy,:autosave =>
true
This should work.
Compare http://mongoid.org/docs/upgrading.html for more details.
So I have one Simple applications, such as RailsCast about nested forms. The problem is that when I submit the form (with survey and questions), the question will not be saved.
My model (survey, there are many Questions):
class Survey
include Mongoid::Document
field :name
has_many :questions, :dependent => :destroy< br /> accepts_nested_attributes_for :questions, :allow_destroy => true
end
class Question
include Mongoid::Document
field :content
belongs_to :survey
end
and survey controller:
def new
@survey = Survey.new
3.times {@survey.questions.build}
....
And an opinion:
<%= form_for(@survey) do |f| %>
<%= f.fields_for :questions do |builder| %>
<%= builder.label :content, "Question" %>
< %= builder.text_area :content, :rows => 3 %>
<%= buil der.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Question" %>
<% end %>
...
In In my log, I have:
Started POST "/surveys" for 127.0.0.1 at 2011-05-24 13:26:51 +0400
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"G£ô", "authenticity_token"=>"tX0FfMiLbh1BwjuY4CuvAKt2UpTraY3vmdo58ocBnos=", "survey"=>{"name"=>"
Rails", "questions_attributes"=>{"0"=>{"content"=>"Are you fond of Rails?", "_destroy"=>"0"}, "1"=>{"content"=> "Rails is
the best, ha?", "_destroy"=>"0"}, "2"=>{"content"=>"How many railscasts have you watched?", "_destroy"=> "0"}}}, "commit
"=>"Create Survey"}
MONGODB nested_attributes_development['surveys'].insert([{"name"=>"Rails", "_id"= >BSON::ObjectId('4ddb79dba5372914380000
69')}])
Redirected to http://localhost:3000/surveys/4ddb79dba537291438000069
The answer was found in the Google team, so I just copied it:
MONGODB
nested_attributes_development['surv eys'].insert([{"name"=>"Rails",
"_id"=>BSON::ObjectId('4ddb79dba5372914380000
69')}])
This will not be saved to the unfinished question collection
mongoid default.
Just add has_many:questions,:dependent =>:destroy,:autosave =>
true
this should work. p>
Refer to http://mongoid.org/docs/upgrading.html for more details.