如何解决ruby-on-rails-3-Mongoid嵌套属性保存失败问题?

2026-04-11 18:331阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计449个文字,预计阅读时间需要2分钟。

如何解决ruby-on-rails-3-Mongoid嵌套属性保存失败问题?

我有一个简单的应用程序,比如RailsCast中的嵌套表单。问题是,当我提交表格(包括检查和问题)时,问题不会保存。我的模型(调查,有多个问题)如下所示:`class Survey include Mongoid::Document field :name has_many :questions`

所以我有一个简单的应用程序,如 RailsCast about nested forms.,问题是,当我提交表格(带调查和问题)时,问题不会保存.

我的模型(调查,有很多问题):

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

和调查控制员:

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

和一个观点:

如何解决ruby-on-rails-3-Mongoid嵌套属性保存失败问题?

<%= 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 localhost:3000/surveys/4ddb79dba537291438000069 答案是在谷歌小组中找到的,所以我只是复制它:

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

这不会保存到未完成的问题集合
mongoid默认.
只需添加has_many:questions,:dependent => :destroy,:autosave =>
真正
这应该工作.

比照mongoid.org/docs/upgrading.html了解更多详情.

本文共计449个文字,预计阅读时间需要2分钟。

如何解决ruby-on-rails-3-Mongoid嵌套属性保存失败问题?

我有一个简单的应用程序,比如RailsCast中的嵌套表单。问题是,当我提交表格(包括检查和问题)时,问题不会保存。我的模型(调查,有多个问题)如下所示:`class Survey include Mongoid::Document field :name has_many :questions`

所以我有一个简单的应用程序,如 RailsCast about nested forms.,问题是,当我提交表格(带调查和问题)时,问题不会保存.

我的模型(调查,有很多问题):

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

和调查控制员:

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

和一个观点:

如何解决ruby-on-rails-3-Mongoid嵌套属性保存失败问题?

<%= 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 localhost:3000/surveys/4ddb79dba537291438000069 答案是在谷歌小组中找到的,所以我只是复制它:

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

这不会保存到未完成的问题集合
mongoid默认.
只需添加has_many:questions,:dependent => :destroy,:autosave =>
真正
这应该工作.

比照mongoid.org/docs/upgrading.html了解更多详情.