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.– Bruno Wego
How will the parameters be used? Theoretically each _params method has a different return so it is possible to juxtapose the two when using.
– rafaelfranca
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.– Bruno Wego
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.– Bruno Wego
@rafaelfranca thanks for the tips, the solution I posted below solves the question, maybe there are better ways...
– Bruno Wego