Search code examples
ruby-on-railsparsingparametersboolean

How do I submit a boolean parameter in Rails?


I'm submitting a parameter show_all with the value true. This value isn't associated with a model.

My controller is assigning this parameter to an instance variable:

@show_all = params[:show_all]

However, @show_all.is_a? String, and if @show_all == true always fails.

What values does Rails parse as booleans? How can I explicitly specify that my parameter is a boolean, and not a string?


Solution

  • I wanted to comment on zetetic answer but as I can't do that yet I'll post this as an answer.

    If you use

    @show_all = params[:show_all] == "1"

    then you can drop ? true : false because params[:show_all] == "1" statement itself will evaluate to true or false and thus ternary operator is not needed.