Florian Dano Clement
Florian Dano Clement

Reputation: 257

Rails Jquery on Checkbox

I begin in Rails and i created an application. In this application , the admin is the only person who can create user, when he signup the others users , he can check an checkbox for choose if he created users or admin, because in my table user , i create a boolean. And he have a page where he can see a tab with the different user. In this page i want to add a checkbox in my tab and the admin can add or remove the admin for the others users.

But I have no idea how create that :>

Here my Controler code :

class RegistrationsController < Devise::RegistrationsController
  before_filter :authenticate_user!, :redirect_unless_admin, :except => [:edit]
  prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]



def create
  @user = User.create
    build_resource
    respond_to do |format|
      format.html {redirect_to :controller => 'users', :action => 'index'}
      format.js
    end
    if resource.save
      redirect_to users_index_path
       flash[:succes] = "Utilisateur cree"
    else
      clean_up_passwords resource
      respond_with resource
      flash[:avertissement] = "Veuillez remplir tout les champs."

    end
  end


def new
  @user = User.new

end

Here my index.html.erb Code :

 <% btn_section = image_tag('btn-section.png') %>
   <% btn_admin = image_tag('checkmark.png') %>
   <% btn_noadmin = image_tag('close.png') %>
  <% btn_eye = image_tag('eye.png') %>
      <% btn_edit = image_tag('pencil.png')%>
      <% btn_remove = image_tag ('remove.png') %>
  <div class="section-admin">
<h1>Users</h1>

 <div class="btn-section-size"> <%= link_to 'Nouvelle Utilisateur', new_user_registration_path ,:class => "btn btn-primary submit-deco"%></div>

                <table class="user-index">

                            <colgroup>
                              <col class="size-Email">
                              <col class="size-admin"
                              <col class="size-action">
                            </colgroup>
                            <thead> <!-- En-tête du tableau -->
                              <tr>
                                  <th>Email</th>
                                  <th>Admin</th>
                                  <th width = "70 px">Actions</th>
                              </tr>
                            </thead>
                            <% @users.each do |user| %>
                            <tbody>
                            <!-- Boucle pour afficher les articles dans le tableau de chaque section -->
                              <tr>
                                  <td><%= user.email %> </td>
                                  <%= simple_form_for(resource_name, :remote => true, :as => resource, :url => user_registration_path()) do |f| %>
                                  <td><%= f.check_box (:admin), :id => "IsAdminSelect" %>

                                    <%#if user.admin? %>
                                    <%#= btn_admin %>
                                    <%# else %>
                                    <%#= btn_noadmin %>
                                    <%#end %><% end %></td>

                                  <td class="actiontab"> <%#= link_to btn_edit, edit_user_registration_path(user) %>
                  <%= link_to t('.destroy', :default =>(btn_remove)),user_registration_path(:id => user.id),
                                  :method => :delete,
                                    :data => { :confirm => t('.confirm', :default => t("helpers.link.confirm", :default => 'Etes vous certains ?')) } %></td>
                                </tr>

<% end %>
                            </tbody>

                          </table>

</div>

and my registrations new code :

<div class="section-index">
<h2 class="title-edit-new">Inscription</h2>

<%= simple_form_for(resource_name, :as => resource, :url => user_registration_path()) do |f| %>
<%= f.error_notification %>
<ul class="form">
  <div class="inputs">
   <li> <%= f.label :email %></li>
   <li><%= f.email_field :email, :required => true, :class =>'email-input', :autofocus => true %></li>
   <li> <%= f.label :password %></li>
    <li><%= f.password_field :password, :class =>'password-input', :required => true %></li>
    <li class="passwordconfirm"> <%= f.label :password_confirmation %></li>
  <li> <%= f.password_field :password_confirmation, :class =>'passwordconfirm-input', :required => true %></li>
  </div>
  <li>
  <%= f.label :admin%>
  <%= f.check_box (:admin) %></li>

  <div class="actions">
   <li> <%= f.button :submit, "Inscription", :class => 'btn btn-primary submit-input'%></li>
  </div>
<% end %>

</ul>
</div>

I hope you can help me. Sorry for my bad english , i stay here for add more information if you need.

EDIT 1 :

I start my script like this

<script type="text/javascript">
$(document).ready(function(){

$(".admin_select").click(function(){


  alert("/users/"+$(this).data("user-id")+"/toggle(:admin)");
});

});

</script>

And edit my index view like this

  <td><%= f.check_box :admin, :checked => user.admin?, :class => "admin_select",
                                                       :data => { :user_id => user.id } %>

Now i can see who are admin or not. So I just need implement the toggle(:admin) in the script.

EDIT 2 :

My routes.rb files

Redmine::Application.routes.draw do
  get "users/index"

root to: "sections#show", :id => '29', :division_id => '5'

resources :users do

  member do
    put :toggle_admin
  end
end


resources :dashboard
 resources :divisions do
  resources :sections do
    resources :articles
    end
  end

devise_for :users, :path_names => { :sign_in => "login"}, :controllers => { :registrations => 'registrations'}

devise_scope :user do
  get "sign_in", :to =>"devise/sessions#new"
end

end

Upvotes: 3

Views: 1170

Answers (1)

Muntasim
Muntasim

Reputation: 6786

Your question is not clear enough to me. Do you want to manage the users in the list page? for example in the list there will be checkbox under the column name admin? , then checking on the link will make him admin and unchecking will make him normal user?

Then:

in your routes.rb

resources :users 

   member do
    put :toggle_admin
   end
end

in your controller

def toggle_admin
   @user = User.find(params[:id])
   @user.toggle!(:admin)
end

in index.html.erb

Replace

<td><%= f.check_box (:admin), :id => "IsAdminSelect" %>

                                <%#if user.admin? %>
                                <%#= btn_admin %>
                                <%# else %>
                                <%#= btn_noadmin %>
                                <%#end %><% end %></td>

with

<td id="admin_user_status_<%=user.id%>"> <%= check_box_tag :admin, 1, user.admin?, update_admin_url: toggle_admin_user_path(user) %></td>

And add the following js

<script type="text/javascript">
$('body').on('click', '[update_admin_url]' , function(){
    $.ajax({
        url: $(this).attr('update_admin_url'),
        type: 'PUT'
    });
});

add a file named /views/users/toggle_admin.js.erb with content as:

$('#admin_user_status_<%= @user.id%>').html("<%= check_box_tag :admin, 1, @user.admin?, update_admin_url: toggle_admin_user_path(@user) %>")

You dont need to have any form in index.html.erb

Let me know if you get this working

Upvotes: 2

Related Questions