How to filter nested parameters from Rails logs
Simply put this in application.rb:
config.filter_parameters += [:data]
This would filter nested [:data] keys also.
In rails 5, you can define hierarchy of the key:
config.filter_parameters += ["download.data"]
This would filter all the [:data] keys which have [:download] as the immediate parent.
Encoding parameters with multiple duplicate keys
Ran into a similar issue, there's a helpful post here Ruby Hash with duplicate keys? but briefly
params = {}.compare_by_identity
params['input'] = "webpage/url:http://www.example.com"
params['input'.dup] = "keywords:some+keywords"
then
params.to_query
returns
"input=keywords%3Asome%2Bkeywords&input=webpage%2Furl%3Ahttp%3A%2F%2Fwww.example.com"
Rails - Parameter with multiple values in the URL when consuming an API via Active Resource
to create a valid query string, you can use
params = {a: 1, b: [1,2]}.to_query
http://apidock.com/rails/Hash/to_query
http://apidock.com/rails/Hash/to_param
Rails - passing parameters in link_to
First of all, link_to is a html tag helper, its second argument is the url, followed by html_options. What you would like is to pass account_id as a url parameter to the path.
If you have set up named routes correctly in routes.rb, you can use path helpers.
link_to "+ Service", new_my_service_path(:account_id => acct.id)
I think the best practice is to pass model values as a param nested within :
link_to "+ Service", new_my_service_path(:my_service => { :account_id => acct.id })
# my_services_controller.rb
def new
@my_service = MyService.new(params[:my_service])
end
And you need to control that account_id is allowed for 'mass assignment'. In rails 3 you can use powerful controls to filter valid params within the controller where it belongs. I highly recommend.
http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods
Also note that if account_id is not freely set by the user (e.g., a user can only submit a service for the own single account_id, then it is better practice not to send it via the request, but set it within the controller by adding something like:
@my_service.account_id = current_user.account_id
You can surely combine the two if you only allow users to create service on their own account, but allow admin to create anyone's by using roles in attr_accessible.
hope this helps
Rails: Preserving GET query string parameters in link_to
link_to 'Link', request.query_parameters.merge({:per_page => 20})
Ruby: Easiest Way to Filter Hash Keys?
Edit to original answer: Even though this is answer (as of the time of this comment) is the selected answer, the original version of this answer is outdated.
I'm adding an update here to help others avoid getting sidetracked by this answer like I did.
As the other answer mentions, Ruby >= 2.5 added the Hash#slice
method which was previously only available in Rails.
Example:
> { one: 1, two: 2, three: 3 }.slice(:one, :two)
=> {:one=>1, :two=>2}
End of edit. What follows is the original answer which I guess will be useful if you're on Ruby < 2.5 without Rails, although I imagine that case is pretty uncommon at this point.
If you're using Ruby, you can use the select
method. You'll need to convert the key from a Symbol to a String to do the regexp match. This will give you a new Hash with just the choices in it.
choices = params.select { |key, value| key.to_s.match(/^choice\d+/) }
or you can use delete_if
and modify the existing Hash e.g.
params.delete_if { |key, value| !key.to_s.match(/choice\d+/) }
or if it is just the keys and not the values you want then you can do:
params.keys.select { |key| key.to_s.match(/^choice\d+/) }
and this will give the just an Array of the keys e.g. [:choice1, :choice2, :choice3]
Related Topics
Ruby: Mass Initializing Instance Variables
Selenium Webdriver Getting a Cookie Value
Native Threads in Ruby 1.9.1, Whats in It for Me
Controller Monkey Patch in Initializer Gets Lost When Rails Reloads Classes
Pass Command Line Argument to Vagrant Shell Script Provisioner
Git Bash Chcp Windows7 Encoding Issue
Access Attributes/Methods Comments Programmatically in Ruby
Search for "Enabled" Users in Net-Ldap for Ruby
Rails Gem to Break a Paragraph into Series of Sentences
Using Passphrase Callback in Ruby Gpgme
Gets.Chomp Without Moving to a New Line
Markdown to Plain Text in Ruby
How to Render a Layout Directly from Routes.Rb, Without a Controller
Get Pry to Display Characters Like [äöüßÄÖÜß] (Utf-8 Encoding)? (Possibly Windows-Specific Issue)