Search code examples
ruby-on-railsbooleanform-helpers

Boolean attribute not being saved via check_box


I have a Job model with an attribute of :rush which is a boolean. I used the check_box form helper to toggle :rush.

The check box form element:

<%= check_box_tag(:rush) %>
<%= label_tag(:rush, "Rush?") %>

My strong parameters method:

def job_params
  params.require(:job).permit(:recipient, :age, :ethnicity, :gender, :height, :weight, :hair, :eyes, :other_info, :instructions, :user_id, :company_id, :case_id, :rush, addresses_attributes:[:id, :label, :addy, :apt, :city, :state, :zip ] )
end

The Create action:

def create
  @job = Job.new(job_params)
  @job.user_id = current_user.id

  if @job.save
    redirect_to current_user
  else
    render 'new'
  end
end

When the form is submitted, it doesn't save :rush yet all other attributes including nested attributes save just fine. Am I missing something here?


Solution

  • Since your function says:

    params.require(:job).permit(:recipient, :age, :ethnicity, :gender, :height, :weight, :hair, :eyes, :other_info, :instructions, :user_id, :company_id, :case_id, :rush, addresses_attributes:[:id, :label, :addy, :apt, :city, :state, :zip ] )
    

    Then you are expecting :rush nested inside :job, that is, params[:job][:rush]. So you should nest it inside the job attribute in your html:

    <%= check_box_tag("job[rush]") %>
    

    this should work.