Проблема вложенных атрибутов в рельсах 3 с монгоидом (вложенные объекты не сохраняются) - PullRequest
3 голосов
/ 24 мая 2011

Итак, у меня есть простое приложение, как в RailsCast о вложенных формах. , и проблема в том, что когда я отправляю форму (с опросом и вопросами) вопросы не сохраняются .

Мои модели (опрос, у которого много вопросов):

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

и Survey Controller:

def new
    @survey = Survey.new
    3.times {@survey.questions.build}
....

и вид:

<%= form_for(@survey) do |f| %>
    <%= f.fields_for :questions do |builder| %>
    <%= builder.label :content, "Question" %><br />
    <%= builder.text_area :content, :rows => 3 %><br />
    <%= builder.check_box :_destroy %>
    <%= builder.label :_destroy, "Remove Question" %>
    <% end %>
...

В моем журнале у меня есть:

    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

1 Ответ

7 голосов
/ 25 мая 2011

Ответ был найден в группах Google, поэтому я просто продублировал его:

MONGODB 
nested_attributes_development['surveys'].insert([{"name"=>"Rails", 
"_id"=>BSON::ObjectId('4ddb79dba5372914380000 
69')}]) 

Это не сохраняет в коллекциях вопросов, которые не выполняются с mongoid по умолчанию.Просто добавьте has_many: questions,: variable =>: destroy, : autosave => true Это должно работать.

ср.http://mongoid.org/docs/upgrading.html для получения более подробной информации.

...