How to add globally allowed parameters in Rails?

Asked

Viewed 116 times

1

In several Sources I use address, I decided to allocate logic in Concerns. A need is to add the allowed address parameters, see below the Concern that should add permission to address_attributes:

module AddressationConcern
  extend ActiveSupport::Concern

  included do
    before_action :address_params
  end

  protected

  def address_params
    params.permit address_attributes: [
      :zip_code, :state, :neighborhood, :city, :street, :number, :complement
    ]
  end
end

Like any Concern, I call the top of the controller:

class UsersController < ApplicationController
  include AddressationConcern

  ...

  private

  ...

  def user_params
    params.fetch(:user, {}).permit :name, :cpf, :email, :password
  end
end

The question is on the second call from params.permit where I overwrite the one contained within the Concern.

I do not know a way to make this kind of logic effective. Some colleague knows a way to make this effective?

Thank you!

  • AddressationConcern is a controller conference, making it clear here.

  • 1

    How will the parameters be used? Theoretically each _params method has a different return so it is possible to juxtapose the two when using.

  • I get it. The idea was just to isolate address_attributes in a conference so you don’t have to keep repeating this in every Source that uses it. But you’re right, we may have different parameters in different methods and that doesn’t apply. Now if there could be a way to inform that that group (address_attributes) is global, that, I believe, could have several applications. But it’s an idea, I could be wrong.

  • I raised this question in order to understand if it is possible, but the simple way is to have address_attributes defined on each controller that will then use it. I love how Concerns can assist in the encapsulation of repeated logic.

  • @rafaelfranca thanks for the tips, the solution I posted below solves the question, maybe there are better ways...

1 answer

2


The best alternative I found, given my need to abstract the address parameter I use in several Sources was to create a module returning an array with the attributes, see below:

module AddressationConcern
  extend ActiveSupport::Concern

  protected

  def address_attributes
    [:zip_code, :state, :neighborhood, :city, :street, :number, :complement]
  end
end

I call the method in user_params:

class UsersController < ApplicationController
  include AddressationConcern

  ...

  private

  ...

  def user_params
    params.fetch(:user, {}).permit(
      :name, :cpf, :email, :password,
      address_attributes: address_attributes
    )
  end
end

I talked to several colleagues and friends about this approach. A Gem strong_parameters is fantastic, this kind of abstraction is unnecessary, but wanted to implement it for reasons of perfectionism.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.