Skip to content
This repository was archived by the owner on Aug 17, 2017. It is now read-only.

Add regex support for permitting params #21

Closed
wants to merge 2 commits into from

Conversation

tjschuck
Copy link

Instead of explicitly enumerating all permitted params as symbols, allow for regexes:

params.permit(/^company_\d$/ => [:name, :location, /^quarter_\d_profit$/])

This also helps when using ActionView DateHelper methods. Instead of:

params[:account].permit(:"trial_til(1i)",
                        :"trial_til(2i)",
                        :"trial_til(3i)",
                        :"trial_til(4i)",
                        :"trial_til(5i)")

you can more tersely use something like:

params[:account].permit(/^trial_til/)

(See pull request #17 for an alternate but less flexible solution to the DateHelper problem.)

@DouweM
Copy link

DouweM commented May 13, 2012

👍

ec7b957 made strong_parameters 1.8 compatible
@tjschuck
Copy link
Author

Added 929332d to revert to 1.8 hash syntax after the merging of pull request #19

@dhh
Copy link
Member

dhh commented Jun 29, 2012

I'd rather we just default to permit :published_at will also just automatically allow :published_at(x) -- instead of all this extra configuration. If you want to take a stab at a pull request that does that, please open a new ticket. I posted the same message to pull request #17.

@dhh dhh closed this Jun 29, 2012
@tjschuck
Copy link
Author

Though this does solve the ActionView DateHelper problem, we use the regex syntax for conveniences beyond just that.

@bjhess
Copy link
Contributor

bjhess commented Jun 30, 2012

@dhh My 2¢.

While I can be convinced that the DateHelper problem specifically could be solved in a more convention-based way, I still think there is tons of value in supporting regex. I'll try to convince you. :)

We have project edit pages that provide a one stop shop for managing budgets, rates and project manager status on all the users assigned to the project. Every single one of these input fields is going to come through the params hash. As a point of reference, here is how you'd look each one up:

params[:user-assignment-1234][:budget]
params[:user-assignment-1234][:hourly_rate]
params[:user-assignment-1234][:is_project_manager]
params[:user-assignment-4567][:budget]
params[:user-assignment-4567][:hourly_rate]
params[:user-assignment-4567][:is_project_manager]
# etc

Being able to do a strong params declaration like the following turns out to greatly reduce the amount of configuration, as it were, that we are doing in our controllers:

params.permit(/^user-assignment-\d+$/ => [:budget, :hourly_rate, :is_project_manager])

This is why we chose the regex route, rather than trying to code up each possible use case (DateHelper, DOM ids, etc). It is extremely flexible.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants