Search code examples
ruby-on-railsrubyruby-on-rails-3.2ruby-on-rails-4strong-parameters

strong parameters permit all attributes for nested attributes


Is there a way in strong parameters to permit all attributes of a nested_attributes model? Here is a sample code.

class Lever < ActiveRecord::Base
 has_one :lever_benefit
 accepts_nested_attributes_for :lever_benefit
end

class LeverBenefit < ActiveRecord::Base
  # == Schema Information
  #  id          :integer          not null, primary key
  #  lever_id    :integer
  #  explanation :text
end

For lever strong parameters i am writing currently this

def lever
 params.require(:lever).permit(:name,:lever_benefit_attributes => [:lever_id, :explanation])
end

Is there a way for nested attributes i can write to permit all attributes without explicitly giving the attributes name like lever_id and explanation ?

Note: Please don't get confused with this question with permit! or permit(:all) this is for permitting all for nested attributes


Solution

  • The whole point of strong parameters is in its name: make your input parameters strong.
    Permitting all the parameters would be a very bad idea, as it would permit anyone to insert values you don't necessarily want to be updated by your users.

    In the example you give, you mention the two parameters you currently need to provide:
    [:lever_id, :explanation].

    If you permitted all the parameters, it would be possible for somebody to change any other value.
    created_at, or lever_id for example.

    This would definitely be a security issue and this is why you should not do it.
    Explicitely specifying all your attributes might seem boring when you do it.
    But this is necessary to keep your application secure.

    Edit: For people downvoting this. This may not be the answer you're looking for, but it is the answer you need.
    Whitelisting all nested attributes is a huge security flaw that strong params is trying to protect you with, and you're removing it.
    Take a look at what lead to building strong_params, and how not using it can be bad for you: https://gist.github.com/peternixey/1978249