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 的值
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)
#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= 都没有其他的属性的。。