新手问题 mongoid save 问题

sanvi · 2012年08月31日 · 最后由 u1346343354 回复于 2012年09月02日 · 3786 次阅读

42: @post = Post.new(params[:post]) 43: @post.created_at = Time.new 45: if @post.save

[1] pry(#)> @post => #

为什么会没有值呢?

我看了下 params => {"author"=>"asdasd fdsfasd", "source"=>"das", "platform"=>"dasdas", "size"=>"das", "watch"=>"dasdas", "description"=>"das", "category_names"=>"audio"} 是有值的

params 的值 应该是

"post" => {"author"=>"asdasd fdsfasd",
"source"=>"das",
"platform"=>"dasdas",
"size"=>"das",
"watch"=>"dasdas",
"description"=>"das",
"category_names"=>"audio"}

你现在的 params[:post] 是空的呀。

#1 楼 @camel => {"utf8"=>"✓", "authenticity_token"=>"wpRSwcGRKGYl53CI6AA3f0JcgTi8ePqy+S46z5dD5Hg=", "post"=> {"author"=>"asdasd ", "source"=>"dasd", "platform"=>"ddd", "size"=>"ddd", "watch"=>"ddd", "description"=>"ddd", "category_names"=>"audio"}, "commit"=>"Create Post", "action"=>"create", "controller"=>"posts"} 我帖的是 post 的值

看下 log 里是怎样的?

Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"wpRSwcGRKGYl53CI6AA3f0JcgTi8ePqy+S46z5dD5Hg=", "post"=>{"author"=>"asdasd fdsfadda", "source"=>"asdas", "platform"=>"asd", "size"=>"asdasd", "watch"=>"asdas", "description"=>"dasd", "category_names"=>"audio"}, "commit"=>"Create Post"} WARNING: Can't mass-assign protected attributes: author, source, platform, size, watch, description MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} (0.6020ms) MOPED: 127.0.0.1:27017 INSERT database=code4_development collection=posts documents=[{"_id"=>"5041a56cc3666e0d04000003", "category_ids"=>[], "tag_ids"=>[], "created_at"=>2012-09-01 00:00:00 UTC}] flags= MOPED: 127.0.0.1:27017 QUERY database=code4_development collection=categories selector={"$query"=>{"name"=>"audio"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 fields=nil (0.4461ms) MOPED: 127.0.0.1:27017 UPDATE database=code4_development collection=posts selector={"_id"=>"5041a56cc3666e0d04000003"} update={"$addToSet"=>{"category_ids"=>"503c666be448126d11000002"}} flags= MOPED: 127.0.0.1:27017 UPDATE database=code4_development collection=categories selector={"_id"=>"503c666be448126d11000002"} update={"$addToSet"=>{"post_ids"=>{"$each"=>["5041a56cc3666e0d04000003"]}}} flags= Redirected to http://localhost:3000/posts/5041a56cc3666e0d04000003 Completed 302 Found in 5107ms

Started GET "/posts/5041a56cc3666e0d04000003" for 127.0.0.1 at 2012-09-01 14:04:28 +0800 Processing by PostsController#show as HTML Parameters: {"id"=>"5041a56cc3666e0d04000003"} MOPED: 127.0.0.1:27017 QUERY database=code4_development collection=posts selector={"_id"=>"5041a56cc3666e0d04000003"} flags=[] limit=0 skip=0 fields=nil (0.3986ms) Rendered posts/show.html.erb within layouts/application (2.7ms) MOPED: 127.0.0.1:27017 QUERY database=code4_development collection=categories selector={} flags=[] limit=0 skip=0 fields=nil (0.6080ms) Completed 200 OK in 13ms (Views: 11.4ms)

#3 楼 @Tony612 WARNING: Can't mass-assign protected attributes: author, source, platform, size, watch, description 这个警告的问题?

#5 楼 @sanivbyfish 有可能。。post 的 model 呢?

#6 楼 @Tony612 class Post include Mongoid::Document field :author, type: String field :source, type: String field :platform, type: String field :size, type: String field :watch, type: Integer field :created_at, type: Date field :updated_at, type: Date field :description, type: String

has_and_belongs_to_many :categories has_and_belongs_to_many :tags

attr_accessor :category_names attr_accessible :category_names after_create :assign_categories validates :category_names, :presence => true

protected def assign_categories names = category_names.split(',') categories << names.map { |name| Category.find_or_create_by(name: name) } end

end

#7 楼 @sanivbyfish 不觉得这段 log 很奇怪吗? MOPED: 127.0.0.1:27017 INSERT database=code4_development collection=posts documents=[{"_id"=>"5041a56cc3666e0d04000003", "category_ids"=>[], "tag_ids"=>[], "created_at"=>2012-09-01 00:00:00 UTC}] flags= 都没有其他的属性的。。

#8 楼 @Tony612 我怀疑是 attr_accessible 这个的问题,我把其他字段都加进去后就能保存进去了

mongoid 里也设定这个么?我也是刚接触不久 把这句注释掉试试

#10 楼 @Tony612 也能保存,貌似加了一个白名单后,其他的字段不加进来就会保存不进来

#11 楼 那现在怎样了?

#12 楼 @Tony612 没问题,东西能存进 mongodb 里面

#14 楼 @Tony612 收藏了,以后用到在说,现在暂时还没这个需求

@Tony612 最后一次 test

需要 登录 后方可回复, 如果你还没有账号请 注册新账号