0

So I have this form in my books view, which shows a select box to choose if a boolean value is either true or false.

But when I submit it, it does not change the boolean value to true, if I select so.

This is my books scheme basicly:

create_table "books", force: true do |t|
    t.string   "name"
    t.integer  "user_id"
    t.boolean  "oppetool",   default: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "count",      default: 0
  end

Why is it not changing the boolean value for my entry?

My view:

<% provide(:title, "Submit a book") %>

<b align="center">Enter the name of a book you want to add into the database and then press 'Submit!'</b>
    <%= form_for(@book) do |f| %>
            <div class="forms">
                        <%= f.text_field :name, placeholder: "Type what you want to say...", autofocus: true %>

                        <%= f.check_box(:oppetool, {}, "True", "False") %>
                        <%= f.submit 'Submit!' %>
                </div>
        <% end %>

Books controller:

class BooksController < ApplicationController

  before_action :signed_in_user, only: [:index,:edit,:update, :destroy]
  before_action :admin_user,     only: :destroy
  before_action :set_book, only: [:show, :edit, :update, :destroy]

  def index
    @books = Book.all
  end

  def show
     @book = Book.find(params[:id])
  end

  def new
     @book = current_user.books.build
  end

  def create
    @book = current_user.books.build(book_params)
    if @book.save
      flash[:success] = "Book listed!"
      redirect_to books_path
    else
      flash[:success] = "Did you leave a field empty? All fields must be filled before we can accept the review!"
      render new_book_path
    end
  end

  def edit

  end

  def update

  end

  def destroy

  end
  # Private section
  private
  def book_params
    params.require(:book).permit(:name, :user_id)
  end

  def user_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

  def admin_user
    redirect_to(root_url) unless current_user.admin?
  end
  # Redirecting not logged in user etc.
  def signed_in_user
    unless signed_in?
      store_location
      redirect_to '/sessions/new', notice: "Please sign in!"
    end

  end

end
2
  • can you show your form view and controller actions please. Commented Jan 18, 2014 at 11:50
  • I added them to my original post. Commented Jan 18, 2014 at 12:07

3 Answers 3

1

First of all, update book_params to allow oppetool

params.require(:book).permit :name, :user_id, :oppetool

Then simply use

<%= f.check_box :oppetool %>

The default setting use "0" for false and "1" for true.

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

Comments

1

You haven't included :oppetools in your list of permitted parameters (book_params).

Comments

1

What prevents the value from changing is this line in your controller logic:

params.require(:book).permit(:name, :user_id)

You essentially block out :oppetool from being changed here. Change this line to

params.require(:book).permit(:name, :oppetool).

I would also remove :user_id here to avoid people mass-assigning this value, which is probably what you want.


By the way, why do you set the two checkbox values yourself, instead of leaving them blank, like this:

<%= f.check_box(:oppetool) %> ?

If you used this, Rails should automagically convert the default values "0" and "1" to the corresponding boolean value. I'm not sure if that works for "True" and "False" properly, too, that's why I'm asking.

2 Comments

Did not see your answer before. Marked other one as solved but thank you! Was a lot of help for me.
You're welcome! Did you see the link for mass-assignment I provided? You might want to read this (make sure to especially read the Rails 4 section). It's VERY important to block out attributes like :user_id!

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.