mercredi 7 septembre 2016

Checkbox returns false when checked instead of true - Rails 4.2


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: votre@mail.com" %>
</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

Aucun commentaire:

Enregistrer un commentaire