【发布时间】:2013-12-27 22:16:16
【问题描述】:
我有一个模型,我允许管理员用户对其进行所有更新。
这是控制器和模型中的相关代码:
private:
class ModelsController < ApplicationController
def model_params
params.require(:model).permit! if current_user.admin?
end
def update
@model = Model.find(params[:id])
respond_to do |format|
if @model.update_attributes(model_params)
format.html { redirect_to @model, notice: 'model was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit",flash:
{error:@model.errors.full_messages.join(', ')} }
format.json { render json: @model.errors, status: :unprocessable_entity }
end
end
end
end
class Model < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
end
然而, 当我从活动管理编辑页面更新属性时,我仍然得到 ActiveModel::ForbiddenAttributesError
相关宝石: Rails4、Ruby2.0、activeadmin
【问题讨论】:
-
我不明白你怎么能断定单个
job_params方法定义是你控制器中唯一的“相关代码”。此方法在更新属性的情况下的使用同样重要。 -
你确定 ActiveAdmin 正在通过 ModelsController 吗?
-
谢谢@PeterAlfvin。添加更新方法的代码
-
您是否 100% 确定用户是管理员?我认为添加 before_filter 并检查用户是否是管理员会更好。 p.s.顶部的“private:”是什么?
-
@muistooshort 感谢您的评论。事实证明,我必须为这样的活动管理员独立允许参数。如果您提交答案,我可以接受。像这样:controller do def allowed_params params.permit!结束结束
标签: ruby-on-rails ruby activeadmin mass-assignment strong-parameters