Hi there,
What is the best way to see if a certain key exists in my params hash in
my controller?
Thanks,
steve
Hi there,
What is the best way to see if a certain key exists in my params hash in
my controller?
Thanks,
steve
params.has_key?(:the_key)
Just print it out…
pp params
But a word of caution - do remove the pp line in production mode as this
can
bring your app down to its knees - “something went wrong”…
In your view, you can do <%= debug(params) %> to see that what all’s
there
in the params hash.
Maku
On Thu, Mar 27, 2008 at 8:49 PM, Steve G. <
Steve G. wrote:
What is the best way to see if a certain key exists in my params hash in
my controller?
params.has_key?(key)
Now read your Ruby tutorial for a little while!
And note that has_key? is sometimes only an intermediate solution, where
you
might really need the more compact expression:
params.fetch(key, ‘default value’)
–
Phlip
मयंक जैन (makuchaku) wrote:
Just print it out…
pp params
Despite you may have answered the wrong question, it bears repeating
here that
nothing compares to this savage technique:
raise params.inspect
Bam - a browser full of nothing but your params. And a stack dump!
–
Phlip
On Thu, Mar 27, 2008 at 9:18 PM, Phlip [email protected] wrote:
raise params.inspect
Bam - a browser full of nothing but your params. And a stack dump!
I do agree that it dumps everything - but at times it helps in
debugging…
–
Maku
Thanks for the tips guys but I am not trying to debug so printing
doesn’t help. I actually have conditional logic in my controller that
depends upon whether or not the key is in the hash. So far the first
solution seems to work.
Phlip wrote:
मयंक जैन (makuchaku) wrote:
Just print it out…
pp params
Despite you may have answered the wrong question, it bears repeating
here that
nothing compares to this savage technique:raise params.inspect
Bam - a browser full of nothing but your params. And a stack dump!
–
Phlip
If you’re looking for one key in particular, you can always:
…do stuff… if params[:certain_key]
Check for empty values if needed.
-Kyle
Steve G. wrote:
Thanks for the tips guys but I am not trying to debug so printing
doesn’t help. I actually have conditional logic in my controller that
depends upon whether or not the key is in the hash. So far the first
solution seems to work.
Are you sure you shouldn’t just use two different routes? Sometimes
routes are
better than extra ?key=value&… parameters!
–
Phlip
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs