0

Rails noob here, how do I get a Rails form to save a boolean checkbox to postgres? I've tried dozens of things I've found online and none of it is working. No matter what I do, the entry in the Postgres DB is "Null". Here is my Model(schema):

ActiveRecord::Schema.define(version: 20171227200151) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "notes", force: :cascade do |t|
    t.string "note_title"
    t.string "note_text"
    t.boolean "note_scratch", default: false, null: false
    t.boolean "note_info"
    t.boolean "note_reminder"
    t.boolean "note_task"
    t.string "note_tags"
    t.date "note_remind_date"
    t.time "note_remind_time"
    t.integer "note_archived"
    t.date "note_archived_date"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

view:

<%= form_with scope: :note, url: notes_path, local: true do |f| %>

  <p>
    <%= f.label :note_title %><br>
    <%= f.text_field :note_title %>
  </p>

  <p>
    <%= f.label :note_text %><br>
    <%= f.text_area :note_text %>
  </p>

  <p>
    <%= f.label :notes_scratch, "Scratch" %>
    <%= f.check_box :notes_scratch %>
    <%= f.label :notes_info, "Info" %>
    <%= f.check_box :notes_info %>
    <%= f.label :notes_reminder, "Reminder" %>
    <%= f.check_box :notes_reminder %>
    <%= f.label :notes_task, "Task" %>
    <%= f.check_box :notes_task %>
  </p>

  <p>
    <%= f.label :note_tags %><br>
    <%= f.text_field :note_tags %>
  </p>

  <p>
    <%= f.label :note_remind_date %><br>
    <%= f.date_field :note_remind_date %>
  </p>

  <p>
    <%= f.label :note_remind_time %><br>
    <%= f.time_field :note_remind_time %>
  </p>

  <%= f.submit %>
<% end %>

Controller:

class NotesController < ApplicationController

  def new
  end

  def create
# for testing
  # render plain: params[:note].inspect
    @note = Note.new(note_params)

    @note.save
    redirect_to @note
  end

  private
    def note_params
      params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
  end

end

If I just render the form results, it will show checked boxes as 1s, but it always comes up Null in the database, even if I set the datatype to Integer (hoping it would store the 1). All of the other fields record just fine. It's only the checkboxes that don't work.

I appreciate any assistance you can lend!

-Brick

1
  • 1
    The answer by @jdgray will probably fix the problem, but when you look at the server response, you should see lines indicating there were unpermitted parameters. That should have been your tip-off. Commented Jan 3, 2018 at 22:10

1 Answer 1

1

Review your note_params (and schema.rb):

def note_params
  params.require(:note).permit(:note_title, :note_text, :note_scratch, :note_info, :note_reminder, :note_task, :note_tags, :note_remind_date, :note_remind_time, :note_archived, :note_archived_date)
end

In your view: notes_* needs to be singular as your schema is note_*.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your time! I'm going to go fix that and hang my head in shame :)
Np, it happens :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.