Checkbox returns false when checked instead of true - Rails 4.2

By : Tekyy
Source: Stackoverflow.com
Question!


So I am trying to build an e-commerce website for a school project with Ruby on Rails, using Devise for the users. I am still learning this language on my own, and i encoutered a problem.

I've got this view generated by devise in : views/devise/registrations/new.html.erb

<%= bootstrap_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.text_field :prenom, autofocus: true %>
 </div>

<div class="field">
  <%= f.text_field :nom, autofocus: true %>
</div>

<div class="field">
  <%= f.text_field :adresse, autofocus: true %>
</div>  

<div class="field">
  <%= f.number_field :cp, autofocus: true %>
</div>

<div class="field">
  <%= f.email_field :email, autofocus: true, placeholder: "Ex: [email protected]" %>
</div>

<div class="field">
  <% if @validatable %>
    <%= f.password_field :password, autocomplete: "off", placeholder: "8 caractères minimum" %>
  <% end %>
</div>

<div class="field">
  <%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="field">
  <%= f.check_box :admin %>
</div>

<div class="actions">
  <%= f.submit "Sign up" %>
</div>

I know I shouldn't give an admin field in my form but this is just for testing purposes. So when I check the checkbox and submit this form, it returns false. I have no idea why. Any help, explanations, is much appreciated.

My user_controller if needed :

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # Fais appel à la méthode permettant de restreindre l'accès uniquement aux admins
  # before_filter :authorize_admin, only: :index

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])
  end

  # GET /users/new
  def new
    @user = User.new
  end

 # GET /users/1/edit
 def edit
 end

 # POST /users
 # POST /users.json
 def create
   @user = User.new(user_params)

   respond_to do |format|
     if @user.save
       format.html { redirect_to @user, notice: 'User was successfully created.' }
       format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render :edit }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
  @user.destroy
  respond_to do |format|
    format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
  @user = User.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.fetch(:user, {})
end
end
By : Tekyy


Answers

I don't see the place in your code where you make use of the :admin checkbox value, but I'm guessing that perhaps you simply ask if that param is true or not, like so?

if params[:user][:admin] == true
  do this...
else
  do this...
end

The problem with this is that the submitted checkbox value is, by default, a "1" if checked (note that it is of type string). A text string of any value is not equal to the boolean value of true or false.

You could update your code to something like:

if params[:user][:admin] == "1"
  do this...
else
  do this...
end

Rails always receives submitted params as string values

By : John


The key bit is right at the end:

# Never trust parameters from the scary internet, only allow the white list through.
def user_params
  params.fetch(:user, {})
end

The whitelist (second argument) is empty so all the parameters are ignored. If you looked at your log file you would see a warning about parameters being ignored (you can configure this to raise an exception).

It isn't clear if the controller being used is your UsersController or devise's registration controller - you've show the view for one but the controller code for the other.

If you are using devise's registration controller, then UsersController may not be relevant - you would normally use a before filter to run something like this

devise_parameter_sanitizer.permit(:sign_up, keys: [:admin, ...])

to change devise's default list of permitted fields.



This video can help you solving your question :)
By: admin