Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:32:33+00:00 2026-06-08T22:32:33+00:00

I want to check values in a user-input hash (parameters passed to a Rails

  • 0

I want to check values in a user-input hash (parameters passed to a Rails controller). I want to protect myself against bogus user input. Object#try protects me against missing input, but what about malformed input?

The simple, unsafe case:

  if params[:credentials][:login] …

Obviously, if the user hasn’t provided the :credentials value, then my attempt to get :login will fail … Nil doesn’t have the method :[]. A very nice solution to this is Object#try:

  if params[:credentials].try(:[], :login) … 

But what if the issue is that the user provided malformed credentials? In particular, one of my users passed an array, rather than a hash. So I still get an exception,

TypeError: can't convert Symbol into Integer

Is there something rather like try(), that turns any bogosity into false/nil?

The closest I could come is

  if begin params[:credentials][:login]; rescue; false; end … 

Which is a bit cluttered — though, I grant, still more compact and yet more general than explicit paranoia:

  if (params.has_key? :credentials and params[:credentials].is_a? Hash and params[:credentials].has_key? :login) … 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:32:35+00:00Added an answer on June 8, 2026 at 10:32 pm

    You should not use begin/rescue statements here, it costs in performance and your intentions aren’t clear. Same with try which is simply a rescue nil in disguise.

    You have to check everything and you check it right. It’s just kind of ugly to mix data check and logic.

    Since I watched Avdi’s Confident Ruby, I changed my way of coding; I suggest you do something like (I just simplified your code a little bit):

    def your_action
      checked_params_for_action do
        #safe params here
        #your_controller_code
      end
    end
    
    private
    
    def checked_params_for_action(&block)
      if (params[:credentials].is_a?(Hash) && params[:credentials][:login])
        yield
      else
        redirect_to root_path, error: "params malformed"
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to get matrix values, rows and coloumn from user input. So i'm
I need to check the user input in the preferences if they want to
Suppose that you want to check what input string a user has entered in
I want to check some values in the content of chrome browser page when
I have two Perl string variables containing date values. I want to check whether
Hi I have a List<decimal> containing values between ]0;1]. I want to check if
i want to check users' input value when he input his name, if his
I want to send the username and password values which user types in html
I want to remove all images from a HTML page (actually tinymce user input)
I have a signup form where I'm validating user input. Here's my controller: <?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.