we use checkbox to submit a boolean data in a form.
In rails, when submit the form, a string with "1" or "0" will be submitted to the controller.
In phoenix, when submit the form, a string with "true" or "false" will be submitted to the controller.
It's fine if we directly create object in the database. Either of value will be store as boolean correctly.
But if we need to use the boolean value in our logic, what's the best way to do it?
- Convert to boolean:
# Ruby code
def create
admin = ActiveModel::Type::Boolean.new.cast(param[:admin])
if admin
....
end
end
- Directly use as string:
# Elixir code
def create(conn, params) do
case params[:admin] do
"true" -> do something
_ -> do others
end
end
- Other better ways?