2

我昨天工作得很好,做了一些改变,我不知道我是怎么弄坏的。我确定这是某个地方的错字,但我看不到它。

对表单中的嵌套模型所做的任何更改都根本没有保存,开发日志显示了正在经历的属性 - 它们似乎采用正确的格式,但它根本没有更新。

我有一个User模型,它的references_oneBiography像这样:

# app/models/user.rb
class User
  include Mongoid::Document
  include Mongoid::Timestamps

  field :first_name, :type => String
  field :last_name, :type => String
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

  references_one :biography

  accepts_nested_attributes_for :biography
  #--snip---
end

# app/models/biography.rb
class Biography
  include Mongoid::Document
  include Mongoid::Timestamps

  field :content, :type => String
  field :role, :type => String
  field :is_crew, :type => Boolean

  referenced_in :user
end

最后是我的表单(我在这里使用简单的表单,但在大多数情况下,它的行为与 formtastic 非常相似):

<%= simple_form_for [:manage, @user], :html => {:multipart => true}, :position => :left do |f| %>
  <h2>Login details</h2>
  <%= f.input :email, :input_html => {:class => "text"} %>      
  <%= f.input :first_name, :input_html => {:class => "text"} %>
  <%= f.input :last_name, :input_html => {:class => "text"} %>

  <div class="biography">
    <h2>Biography</h2>
    <%= f.simple_fields_for :biography do |biography_form| %>
      <%= biography_form.input :role, :input_html => {:class => "text"} %>
      <%= biography_form.input :content, :as => :text, :input_html => {:class => "textarea"} %>
      <%= biography_form.input :is_crew, :as => :boolean %>
    <%- end -%>
  </div>

  <%= f.submit "Save user", :class => "submit mid" %>
<% end %>

以及我的开发日志中的一些输出,以防答案在那里,而我只是看不到它们:

    Started POST "/manage/users/john-doe" for 127.0.0.1 at Wed Dec 15 11:42:09 +1100 2010
      Processing by Manage::UsersController#update as HTML
      Parameters: {"commit"=>"Save user", "authenticity_token"=>"44QlHsbKb8Pm91wnxWJa8Y0QsUXDzp/3rVpfs3G1Inc=", "utf8"=>"✓", "id"=>"john-doe", "user"=>{"biography_attributes"=>{"is_crew"=>"0", "role"=>"Tenor", "id"=>"4d080de56a4f1dfe7700000e", "content"=>"John was born on the 1st of January, 1970."}, "last_name"=>"Doe", "first_name"=>"Johnathan", "email"=>"testing@involved.com.au"}}
    the_idea_of_north_development['users'].find({:_id=>BSON::ObjectId('4d06e6036a4f1dcb1b000001')}, {}).limit(-1)
    the_idea_of_north_development['users'].find({:slug=>"john-doe"}, {}).limit(-1)
    the_idea_of_north_development['biographies'].find({"user_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {}).limit(-1)
    the_idea_of_north_development['$cmd'].find({"count"=>"users", "query"=>{:_id=>{"$ne"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, :email=>/^testing@involved\.com\.au$/i}, "fields"=>nil}, {}).limit(-1)
    the_idea_of_north_development['users'].find({"slug"=>"johnathan-doe"}, {})
    MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}})
    Redirected to http://lvh.me:3000/manage/users
    Completed 302 Found in 17ms

对模型的任何更改都可以User更新,但对 的更改Biography未保存。帮助我蜂巢思维,你是我唯一的希望!

4

2 回答 2

0

我一直有一个类似的问题,对嵌套文档的更改没有被持久化。(问题是如何使用 Mongoid 在 Rails 中更新嵌套的 Mongo 文档属性?)。我对 Mongoid 本身持怀疑态度,因为从未记录如下消息来更新嵌入式文档:

MONGODB the_idea_of_north_development['users'].update({"_id"=>BSON::ObjectId('4d080de06a4f1dfe7700000d')}, {"$set"=>{"slug"=>"johnathan-doe", "updated_at"=>Wed Dec 15 00:42:09 UTC 2010, "first_name"=>"Johnathan"}})

如果尝试嵌套文档进行更新,则会出现类似于选择父文档并设置嵌套文档值的行,例如:

the_idea_of_north_development['users'].update({"_id"=>PARENT_DOCUMENT}, {"$set"=>{"biography_attributes.is_crew"=>"0", "biography_attribute.role"=>"Tenor", etc...})

就个人而言,我发现Mongoid Issue #357可能是一个原因。

于 2010-12-16T21:05:34.197 回答
0

在同一问题上杀死了两个星期。我设法做的唯一修复如下。在用户控制器中,执行以下操作:

if @user.save
params[:user][:biography_attributes].each do |bio|
         @biography = @user.biography.build({ :role => bio[:role], :iscrew => bio[:iscrew] })
    end

您将遍历所有传记实例并将其显式保存到数据库中。

于 2015-11-01T10:42:26.643 回答