2

A series of parameters are being submitted to a Rails controller:

"25"=>"true", "30"=>"false", "35"=>"true", "40"=>"true", "45"=>"true", "50"=>"true", "55"=>"true", "60"=>"true", "65"=>"false", "70"=>"true", "75"=>"true", "80"=>"false", "100"=>"true"

The goal is to loop through:

@age_limits = [25,30,35,40,45,50,55,60,65,70,75,80,100]

and process based on each param's value.

The problem is that the syntax for referring to the param is evading me:

@age_limits.each do |limit|
  puts params[:"#{limit}"]
  if params[:"#{limit}"] == "true"
    session[:demographic_agegroups] << params[:"#{limit}"]
    puts session[:demographic_agegroups]
  end
end

Both params[:"#{limit}"] and params[":#{limit}"] do not allow to access the parameter. The former will return the value by putting into console (editor BBEdit does not consider it properly formed Ruby), but will not allow to process in its basis.

How can the controller properly access the param and process based on its value?

1
  • What about to params["#{limit}"] ? From your output, its simple string and you can use limit.to_s Commented Sep 4, 2020 at 5:57

1 Answer 1

2

You'r missing to convert int to string.

Since your params keys are string, you can use params[limit.to_s].

Also, if its key based "keyX".to_sym would be :keyX

Sign up to request clarification or add additional context in comments.

1 Comment

Useful. Succinct. I am surprised that a symbol that gets handled that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.