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