首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >葡萄: ActiveModel::ForbiddenAttributesError

葡萄: ActiveModel::ForbiddenAttributesError
EN

Stack Overflow用户
提问于 2015-10-01 09:53:34
回答 1查看 484关注 0票数 1

当调用我的API端点时,我得到了以下错误:

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):

day_points_api.rb

代码语言:javascript
复制
module V1
  class DayPointsApi < Grape::API
    namespace 'api/v1' do
      resource :points do
        desc 'start all metrik jobs'

        params do
          requires :product, type: String
          requires :type, type: String
          requires :value_at, type: Date

          requires :points, type: Array do
            requires :platform, type: String
            requires :country, type: String
            requires :value, type: Float
          end
        end

        post do
          params[:points].each do |point|
            point_params = point.merge(params.except(:points))
            DayPoint.constantize.import(point_params)
          end
        end
      end
    end
  end
end

显然,这要归功于StrongParameter --但老实说,我已经定义了所需的参数--这些应该是默认允许的唯一参数。

有一些可用的使用辅助方法解决方案--我发现这些解决方案很难看。

这怎麽可能?还有其他选择吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-01 09:53:34

在搜索了其他地方的互联网之后,我在官方葡萄博士上找到了解决方案--真棒!/sarcasm!

如果Rails的版本是4.0+,并且应用程序使用ActiveRecord的默认模型层,则需要使用属性宝石。这个gem在模型层禁用了strong_params的安全特性,允许您使用葡萄自己的params验证。

我加入这个是为了帮助那些应该像我这样绊倒的人。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32884569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档