标签: strong-parameters

Rails ActiveModel :: ForbiddenAttributesError Devise,Omniauth

我遇到了Omniauth身份验证和Rails 4的问题,我得到了一个Rails ActiveModel :: ForbiddenAttributesError.

我使用gem 'protected_attributes'如此强大的参数应该不是问题.

我的用户模型包含以下内容:

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.username = auth.info.email
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.name = auth.info.name
    end
  end
Run Code Online (Sandbox Code Playgroud)

user.password 在那里只是为了保持与现有的Devise auth系统的兼容性.

AR错误表示此行:where(auth.slice(:provider, :uid)).first_or_create do |user|抛出错误.

从以下方法调用上述方法:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def mavenlink
    @user = User.from_omniauth(request.env['omniauth.auth'])
    service = @user.services.initialize_or_update_via_omniauth(request.env['omniauth.auth'])
    if service && service.save
      sign_in_and_redirect @user #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Mavenlink") if is_navigational_format?
    else
      redirect_to root_path, error: "Error signing in …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails devise omniauth strong-parameters

2
推荐指数
1
解决办法
1649
查看次数

Rails:使用curl/Postman发送PUT请求来更新属性

我正在阅读 Michael Hartl 的 Rails 教程书,在强参数部分它说

恶意用户可以发送 PATCH 请求,如下所示:

补丁/users/17?admin=1

此请求将使用户 17 成为管理员,这将是一个潜在的严重安全漏洞。

然后我决定自己做一个“恶意用户”,以便更好地理解它。

因此,我将 :admin 添加到用户控制器中允许的属性列表中,并在 Linux 中使用curl 发送 PUT/PATCH 请求,并在控制台中查看 admin 属性是否确实通过发送恶意请求得到更新。

curl -X PUT http://localhost:3000/users/17?admin=1
Run Code Online (Sandbox Code Playgroud)

首先它抱怨“无法验证 CSRF 令牌的真实性”,然后我注释掉了

# protect_from_forgery with: :exception
Run Code Online (Sandbox Code Playgroud)

在应用程序控制器中,还消除了一些 before_actions 只是想让它工作,但我陷入了它抱怨的地方

ActionController::ParameterMissing (param is missing or the value is empty: user):
  app/controllers/users_controller.rb:49:in `user_params'
  app/controllers/users_controller.rb:37:in `update'
Run Code Online (Sandbox Code Playgroud)

然后我意识到我可能必须将用户参数作为参数哈希值的哈希值传递才能使其工作。如何在curl或Postman(chrome REST客户端)中实现此目的?

ruby curl ruby-on-rails strong-parameters postman

2
推荐指数
1
解决办法
3941
查看次数

为什么 Rails 中的强参数顺序很重要?

在我的product_model_controller.rb强参数中,我有以下代码:

  def product_model_params
    params.require(:product_model)
          .permit(:name, :product_category_id, 
                  product_category_attributes: [:id, :name], attr_val_ids: [])
  end
Run Code Online (Sandbox Code Playgroud)

按照它的方式,它工作得很好。但是,如果我更改参数的顺序,它就会停止工作。例子:

  def product_model_params
    params.require(:product_model)
          .permit(:name, product_category_attributes: [:id, :name],
                  :product_category_id, attr_val_ids: [])
  end
Run Code Online (Sandbox Code Playgroud)

错误:

语法错误,意外的 ',',期望 => ..., :name], :product_category_id, attr_val_ids: []) ... ^

为什么会出现这种情况?我已经坚持了很长时间了:/


产品模型.rb

class ProductModel < ApplicationRecord
  validates :name, presence: true
  validates :name, uniqueness: true

  has_many :products
  has_many :product_model_attr_vals
  has_many :attr_vals, through: :product_model_attr_vals
  has_many :attrs, through: :attr_vals

  belongs_to :product_category

  accepts_nested_attributes_for :product_model_attr_vals
  accepts_nested_attributes_for :product_category
end
Run Code Online (Sandbox Code Playgroud)

产品类别.rb

class ProductCategory < ApplicationRecord
  validates :name, presence: true
  validates …
Run Code Online (Sandbox Code Playgroud)

forms ruby-on-rails nested-forms nested-form-for strong-parameters

2
推荐指数
1
解决办法
301
查看次数

ForbiddenAttributesError,仅在测试中,在次要版本升级后

我正在尝试从Rails 4.1.11升级到4.1.16.升级后,我的几个SessionsController规范都失败了.但是当我尝试登录开发环境时,一切似乎都运行正常.

我有以下代码:

# sessions_controller.rb
def create
  @identity = Identity.find_or_create(auth)
  ...
end

protected

def auth
  request.env["omniauth.auth"]
end

# identity.rb
class Identity < ActiveRecord::Base
  ...
  def self.find_or_create(auth)
    where(auth.slice(:uid, :provider)).first_or_create
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的规范中,我使用fixture spec_helper.rb来提供OmniAuth哈希:

# spec_helper.rb
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
  :provider => 'facebook',
  :uid => '1234567',
  :info => {
    :email => 'joe@bloggs.com',
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用OmniAuth属性来查找用户的身份时,我得到一个ForbiddenAttributesError:

1) SessionsController GET :create not yet signed in sets the user ID in the session hash
   Failure/Error: get :create
   ActiveModel::ForbiddenAttributesError:
     ActiveModel::ForbiddenAttributesError
   # /Users/pat/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.6/lib/active_model/forbidden_attributes_protection.rb:21:in `sanitize_for_mass_assignment' …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails omniauth strong-parameters

1
推荐指数
1
解决办法
573
查看次数

Rails 4 + Devise:如何清理多个新参数?

作为Rails的新手,我正在学习如何实现Devise.

为此,我将于2015年3月26日开始关注Ilya Bodrov-Krukowski的Site Point教程.

本教程教授的内容之一是如何使用附加字段创建注册表单:name.

在更新相关视图后,作者解释了以下内容:

如果您正在使用带有strong_params的Rails(默认情况下在Rails 4中启用),则需要再执行一步:该:name属性必须列入白名单.

他建议这段代码这样做:

before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) << :name
  devise_parameter_sanitizer.for(:account_update) << :name
end
Run Code Online (Sandbox Code Playgroud)

这工作得很好......但我的问题是我想在注册表单中添加多个新字段.

实际上,我希望我的注册表格要求用户输入他们的first_name,last_namecompany_name.

我更新了我的视图,我确实在表单中找到了正确的字段.

但是,到目前为止,我还没有能够将这三个属性列入白名单.

我试着这样做:

before_action :configure_permitted_parameters, if: :devise_controller?

    protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) << :first_name, :last_name, :company_name
      devise_parameter_sanitizer.for(:account_update) << :first_name, :last_name, :company_name
    end
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我得到一个unexpected ","错误.

我也尝试过:

before_action :configure_permitted_parameters, if: :devise_controller?

    protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :company_name) }
  devise_parameter_sanitizer.for(:account_update) { |u| …
Run Code Online (Sandbox Code Playgroud)

attributes ruby-on-rails devise strong-parameters ruby-on-rails-4

1
推荐指数
1
解决办法
1256
查看次数

使用rails 5合并ActionController :: Parameters

在rails 4中,我可以合并!StrongParams,但因为rails 5(beta1)合并!不可用.这是在控制器中执行此操作的最佳方法

  params = ActionController::Parameters.new({
             name: 'Francesco',
             age:  22,
             role: 'admin'
         })
         params.merge!(city: "Los Angeles")
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails actioncontroller strong-parameters ruby-on-rails-5

1
推荐指数
1
解决办法
8105
查看次数

如何使用*键=&gt;值允许散列?

我想创建一个具有强大参数的对象,可以接受动态哈希键。

这是我的代码

Quiz.create(quiz_params)


def quiz_params
  params.require(:quiz).permit(:user_id, :percent, :grade, questions: {})
end
Run Code Online (Sandbox Code Playgroud)

传入的数据看起来像这样。

// the keys that get passed into question is always different

quiz: {
  user_id: 1,
  percent: 80,
  grade: "B",
  questions: {
    "12": "24",
    "1": "12",
    "4": "3",
    "5": "22"
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,当前,当我尝试创建测验时,问题哈希结果为空。

hash ruby-on-rails param strong-parameters

1
推荐指数
2
解决办法
5838
查看次数

强参数:允许符号数组

我需要在我的Rails应用程序中为一个表单列入一长串params列入白名单.我制作了一系列符号,每个符号都有一个我需要列入白名单的参数名称.有没有办法可以将这个数组传递给permit方法并允许这些参数?表单不会将params作为哈希传递.

form.rb

class Form < ApplicationRecord

  jsonb_accessor :fields,
                 salutation: :string, # personal info
                 first_name: :string,
                 last_name: :string,
                 birthday: :string,
                 marital_status: :string,
                 number_of_dependants: :string,
                 first_time_owner: :string,
                 spouse_deal: :string,
                 phone_cell: :string, # contact info
                ...

      def self.fields
        [:salutation, :first_name, :last_name,
         :birthday,
         :marital_status,
         :number_of_dependants,
         :first_time_owner,
         :spouse_deal,
         :phone_cell,
         :phone_home,
        ...
        ]
      end

end
Run Code Online (Sandbox Code Playgroud)

forms_controller.rb

  def form_params
    params.require(:form).permit(:name, Form.fields)
  end
Run Code Online (Sandbox Code Playgroud)

我正在使用jsonb_accessor gem将这些字段存储在Form表的JSON列中.所以我会有很多不同的形式,每个都有不同的参数.所以我需要找到一种动态允许参数的方法.我认为以上可能是一个好的解决方案.虽然我很欣赏有关更好解决方案的建议.

ruby ruby-on-rails strong-parameters

1
推荐指数
1
解决办法
624
查看次数

Rails检查控制器中是否存在params

我有一个factor控制器,它采用两种不同类型的因子的因子值

  1. 主要因素
  2. 次要因素

我将它们作为参数传递如下:

class FactorController < AdminController

  def create
    if primary_factor_params                              ## LINE 5
      do something
    elsif secondary_factor_params
      do something else
    end
  end

  def primary_factor_params
    params.require(:primary).permit(:user_id,            ## LINE 70
                                           :primary_factors)
  end

  def secondary_factor_params
    params.require(:secondary).permit(:user_id,
                                        :secondary_factors)
  end
end
Run Code Online (Sandbox Code Playgroud)

但是在上面我试图传递一个时,secondary_factor我得到以下错误:

ActionController::ParameterMissing (param is missing or the value is empty: primary):
  app/controllers/factors_controller.rb:70:in `primary_factor_params'  app/controllers/api/v1/admin/factors_controller.rb:5:in `create'
Run Code Online (Sandbox Code Playgroud)

所以对我来说似乎这个错误正在出现,因为我primary_factor_params在这种情况下没有任何值,并且由于第一个if条件,它会抛出错误.

我试过了:

primary_factor_params.exists?
primary_factor_params.has_key?(:primary_factors)
....
Run Code Online (Sandbox Code Playgroud)

但是由于primary_factor_params不存在,所有这些都会抛出相同的错误.有没有办法测试这个而不会丢失错误的参数错误?

ruby ruby-on-rails strong-parameters ruby-on-rails-4

1
推荐指数
1
解决办法
5158
查看次数

不同控制器方法的强参数

我正在Rails中创建一个控制器,我正在寻找方法为不同的控制器方法提供不同的强参数

在更新和新操作中,我想要求 post

params.require(:post).permit(:body, :is_public, :title, :id)
Run Code Online (Sandbox Code Playgroud)

但在post/index,我不需要这些参数.

对于不同的控制器方法,如何为不同的要求提供强参数?

ruby-on-rails strong-parameters

1
推荐指数
1
解决办法
348
查看次数