Ruby on rails Rails:质量分配假阴性和重定向
根据之前关于StackOverlfow的问答,我在application.rb中添加了以下内容:Ruby on rails Rails:质量分配假阴性和重定向,ruby-on-rails,variable-assignment,Ruby On Rails,Variable Assignment,根据之前关于StackOverlfow的问答,我在application.rb中添加了以下内容: config.active_record.whitelist_attributes = false 由于我遇到类型错误,无法批量分配受保护的属性 在我这么做之后,似乎一切都很顺利。我现在得到了同样的错误,但它是一个假阴性。请注意,即使我得到了一个错误,该列实际上是更新的 以下是调试器输出: Started PUT "/categories/5" for 127.0.0.1 at 2012-07-
config.active_record.whitelist_attributes = false
由于我遇到类型错误,无法批量分配受保护的属性
在我这么做之后,似乎一切都很顺利。我现在得到了同样的错误,但它是一个假阴性。请注意,即使我得到了一个错误,该列实际上是更新的
以下是调试器输出:
Started PUT "/categories/5" for 127.0.0.1 at 2012-07-09 11:26:40 -0700
Processing by CategoriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SifcfX29c+mGRIJXvUWGnZ8mBelMm4uZloYsoO317SY=", "admin_selections"=>{"admin1"=>"56", "admin2"=>"55", "admin3"=>"", "admin4"=>"", "admin5"=>"", "admin6"=>"", "admin7"=>"", "admin8"=>""}, "category"=>{"update_admins_field"=>"1"}, "commit"=>"Update Category", "id"=>"5"}
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 5 LIMIT 1
(0.1ms) BEGIN
(0.2ms) UPDATE `categories` SET `admins` = '[\"56\",\"55\",\"\"]', `updated_at` = '2012-07-09 18:26:40' WHERE `categories`.`id` = 5
(1.3ms) COMMIT
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 5ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: utf8, _method, authenticity_token, category, commit, action, controller, id):
app/controllers/categories_controller.rb:74:in `block in update'
app/controllers/categories_controller.rb:62:in `update'
看起来好像MySQL代码已经正确生成,但随后出现了回滚和500错误
以下是categories_controller.rb中的相关代码:
def update
@category = Category.find(params[:id])
respond_to do |format| #this is line 62
if params[:category][:update_admins_field]
params['admins'] = return_admins_json (params)
if @category.update_attribute(:admins,params['admins'])
format.html { redirect_to @category, notice: 'Category was successfully updated.' } #line 66
format.json { head :no_content }
end
else
format.html { redirect_to @category, notice: 'Category was not successfully updated.' }
format.json { head :no_content }
end
if @category.update_attributes(params) #line 74
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
为什么要到74号线?用户不应该在第66行被重定向吗?为什么更新时我也会出错?出于安全原因,您应该将白名单属性设置为true,并在每个模型中使用attr\u accessible。有关资料如下: 另外,请阅读上面Niiru的声明 编辑: 在你的控制流中,我认为它没有做你想做的事情。如果它到达第74行,它可能通过了第一个If条件
if params[:category][:update_admins_field]
然后第二次失败
if @category.update_attribute(:admins,params['admins'])
然后退出if/else/end并继续到第74行,因为还没有调用返回。为了解决这个问题,我想你需要这样的东西:
if admin category
if update admin
return success
else
return failure
end
else
if update normal
return success
else
return failure
end
end
第74行不应该改为:
if@category.update_attributes(params[:category])
?这是一个更新操作,模型将使用整个params结构来更新第74行中的记录。这是正确的语法。如果我只更新admins列,前面的条件代码就会启动。执行第二条if语句的原因是它是respond\u to\u format块的一部分。我忽略了使第一个和第二个if块相对于父respond_to_格式有条件。未将category对象与params对象分离会导致请求更新不存在的属性(utf8、方法等)。PS:您需要接受答案,以便我可以将其标记为completewhitelist_属性在GitHUB上附加后默认设置为true(Rails 3.2.3)。请参阅我对niiru的评论。请参阅上面的更新答案。我希望有帮助。我还强烈建议您使用rails来保护您的属性,并使用attr_accessible和whitelisting。我正在使用attr_accessible,但问题是当我试图只保存一个属性时,它认为“method”、“utf8”等也是属性(参见错误消息)。因此niiru发表了评论。选择要传递给更新属性的参数,特别是params[:category]。下面是一个关于用户注册的教程,但想法是一样的: