Chloe
Chloe

Reputation: 26264

How do I allow all parameters even if they are blank?

I have the following:

def index
  @dealFilter = DealFilter.new params[:deal_filter].permit(deal_filter: {})

It works when parameters are given, but not when they are missing. It gives undefined method ``permit' for nil:NilClass Did you mean? print error. I would like it to work in both cases.

Upvotes: 0

Views: 443

Answers (1)

Thanh
Thanh

Reputation: 8604

If params is blank, we need to set a default value for it using fetch:

params.fetch(:deal_filter, {}).permit(:some_params)

so that params[:deal_filter] will be {} when it is missing.

From your errors, the final code should be:

params.fetch(:deal_filter, {}).permit(:min, :max, :sector_id)

Upvotes: 3

Related Questions