Search code examples
ruby-on-railsstrong-parameters

Rails passing string or hash strong parameters


Can I specify using strong parameters either a string or a hash

I have an endpoint for which payload can be either:

card: { number: "4111****", exp_year: "12"}

or

card: "some_encrypted_card"

in strong parameters

If I use permit(:card) case 1 doesn't work If I use require(:card).permit(:number, :exp_year) case 2 doesn't work

Any suggestions?


Solution

  • You could interrogate the card param to see what type it returns:

    def card_params
      if params[:card].is_a? String
        params.permit(:card)
      else
        params.require(:card).permit(:number, :exp_year)
      end
    end