1
我有一个网站,人们可以评论,每个评论都有一个类别,他们可以从下拉菜单中选择。 在我的评论模式我有活动记录确实使用新/创建的所有字段
belongs_to :category
,我必须在注释表CATEGORY_ID。
当用户提交参数中的评论我得到 params [:comment] = {“locale”=>“en”,“body”=>“fds”,“category_id”=> 2,“from_identifier “=> 2130706433,”from_type“=>”ip“,”cookie_user_token“=>”130784267178572“,”user_id“=> 3}
这正是我想要的。然而,当我做
Comment.create(params[:comment])
我得到
#<Comment id: nil, from_type: "ip", from_identifier: 2130706433, cookie_user_token: 130784267178572, body: "fds", locale: "en", positive_vote_count: 0, adjusted_positive_vote_count: 0.0, negative_vote_count: 0, adjusted_negative_vote_count: 0.0, flag_vote_count: 0, adjusted_flag_vote_count: 0.0, impression_count: 0, visit_count: 0, rank: 137.0, created_at: nil, updated_at: nil, user_id: 3, category_id: nil>
,你可以看到CATEGORY_ID为零。
用户模型与活动记录有相同的关系,所以我不知道为什么会得到保存,category_id没有。
现在我做
comm = Comment.create(params[:comment])
comm.category_id = params[:comment][:category_id]
知道为什么,我应该做些什么,以避免黑客那里?
啊我做到了!我错误地用attr_accessor。接得好!谢谢 :)! – Matilda