Mel
Mel

Reputation: 2685

Rails - simple form - boolean attribute not accepting false/no radio button choice

I am making a rails app using simple form (with bootstrap).

I have this question in my form:

<%= f.input :ethics, as: :radio_buttons, label: 'Is an ethics review relevant?' %>

The ethics attribute is a boolean in my table.

The form renders with radio buttons for yes/no.

The default shows as no - but if that's the answer I want to give I'm stuck because I can't submit the form. I get an error that says an answer is required.

Why doesn't simple form acknowledge false as a boolean answer?

The log shows:

Processing by ProjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"", "project"=>{"title"=>"asdfsdf ", "byline"=>"", "description"=>"asdfsdf ", "problem"=>"", "solution"=>"", "remark"=>"",  "ethics"=>"false", 

Upvotes: 3

Views: 1962

Answers (1)

coorasse
coorasse

Reputation: 5528

You probably just forgot to add the ethics field to the list of permitted attributes in your controller.

params.require(:project).permit(..., :ethics)

You should also have no presence validation in the model. Because false is false and will return false for the validation.

Use

validates :ethics, inclusion: { in: [ true, false ] }

Upvotes: 6

Related Questions