简体   繁体   中英

Rails- Checkbox not turning true

I've added a checkbox to my Rails project. When the user marks tradeablestatus while creating a book, it should write true, but it is happening false regardless.

books/_form.html.erb

<%= form_with(model: book, local: true) do |form| %>
  <% if book.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(book.errors.count, "error") %> prohibited this book from being saved:</h2>

      <ul>
        <% book.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <p> If you want your book to be available, tick the status and if you want your book to be tradeable, tick the tradeablestatus. </p>
  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">
    <%= form.label :author %>
    <%= form.text_field :author %>
  </div>

  <div class="field">
    <%= form.label :pagecount %>
    <%= form.number_field :pagecount %>
  </div>

  <div class="field">
    <%= form.label :status %>
    <%= form.check_box :status %>
  </div>

    <div class="field">
    <%= form.label :tradeablestatus %>
    <%= form.check_box :tradeablestatus %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

tradeablestatus migration (I added the tradeablestatus later as a migration. But I don't know if I should add this to book migration)

class AddTradeableStatusToBooks < ActiveRecord::Migration[6.0]
  def change
    add_column :books, :tradeablestatus, :boolean
  end
end

schema.rb (I suspect it is caused by default: false, null: false)

ActiveRecord::Schema.define(version: 2020_10_28_092816) do

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

  create_table "books", force: :cascade do |t|
    t.string "title"
    t.string "author"
    t.integer "pagecount"
    t.boolean "status"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "user_id"
    t.string "username"
    t.boolean "tradeablestatus", default: false, null: false
  end

  create_table "comments", force: :cascade do |t|
    t.string "title"
    t.string "content"
    t.bigint "user_id", null: false
    t.bigint "book_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.boolean "status", default: false, null: false
    t.index ["book_id"], name: "index_comments_on_book_id"
    t.index ["user_id"], name: "index_comments_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "username"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "comments", "books"
  add_foreign_key "comments", "users"
end

Hello) 👋 If u want to receive "true" or "false" on your back-end side u should check this first https://apidock.com/rails/ActionView/Helpers/FormHelper/check_box .

There are smth like check_box("puppy", "gooddog", {}, "yes", "no") .

Yes and no means that if checkbox are checked then yes value goes to back-end side otherwise no goes to back-end side. U should replace this values with yours) If problem will not go then post your controller code please)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM