Reputation: 8461
How can I strip characters from params? If my params look like this:
<ActionController::Parameters {"utf8"=>"✓", "q"=><ActionController::Parameters {"search_fields_cont_any"=>"123-456", "category_eq"=>"", "created_at_gteq"=>"", "created_at_lteq"=>""} permitted: false>, "controller"=>"submissions", "action"=>"index", "page"=>nil, "per_page"=>nil} permitted: false>
params[:q][:search_fields_cont_any] => "123-456"
How can I modify the params so that it would look like this:
params[:q][:search_fields_cont_any] => "123456"
@q = Submission.submissions_for(user: current_user).ransack(filter_params(params[:q]))
def filter_params(params)
params.require(:q).permit(:search_fields_cont_any)
end
Ideally I would modify the params in the private method. How can I do this?
Upvotes: 2
Views: 572
Reputation: 20263
How about:
def filter_params(params)
params.require(:q).permit(:search_fields_cont_any)[:search_fields_cont_any].gsub!('-','')
end
In console:
params = ActionController::Parameters.new(q: {search_fields_cont_any: 'abc-def'})
=> {"q"=>{"search_fields_cont_any"=>"abc-def"}}
params.require(:q).permit(:search_fields_cont_any)[:search_fields_cont_any].gsub!('-','')
=> "abcdef"
Or, as mudasobwa says (even better - saves you 1 character):
def filter_params(params)
params.require(:q).permit(:search_fields_cont_any)[:search_fields_cont_any].delete!('-')
end
Again, in console:
params = ActionController::Parameters.new(q: {search_fields_cont_any: 'abc-def'})
=> {"q"=>{"search_fields_cont_any"=>"abc-def"}}
params.require(:q).permit(:search_fields_cont_any)[:search_fields_cont_any].delete!('-')
=> "abcdef"
Upvotes: 1