2

私は昨日これをうまく機能させ、いくつかの変更を加えました、そして私はそれをどのように壊したのか分かりません。どこかでタイプミスだと思いますが、見えません。

フォーム内のネストされたモデルに加えられた変更はまったく保存されておらず、開発ログには通過中の属性が表示されます。これらは正しい形式であるように見えますが、まったく更新されていません。

私は次のようなUserreferences_oneのモデルを持っています:Biography

# 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

ネストされたドキュメントへの変更が永続化されないという同様の問題が発生しています。(質問は、RailsでネストされたMongoドキュメント属性をMongoidで更新するにはどうすればよいですか?)埋め込まれたドキュメントを更新するために次のようなメッセージがログに記録されることはないため、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

同じ問題で2週間殺された。私が何とかした唯一の修正は次のとおりです。ユーザーコントローラーで、次の手順を実行します。

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

伝記のすべてのインスタンスを繰り返し処理し、それをDBに明示的に保存します。

于 2015-11-01T10:42:26.643 に答える