I have been struggling with creating the Roles in my application, I have installed the gem Rolify and it created a model Role in which this is the code:
class Role < ActiveRecord::Base has_and_belongs_to_many :users, :join_table => :users_roles has_many :abilities end
It also created automatically the joined table users_roles which stores the id of the user and the id of the role that exist in my database. I created 3 roles Admin
- Patient
- Doctor
. My question is how could I pass values to this :users_roles table with a check-boxes in the registration%% The code in my registration how could I modify it
<div class="form-group"><%= f.label :roles %><% Role.all.each do |role| %><%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include(role.id)%><%= role.name %><br /><% end %></div>
So that in the users_roles table every time i Check Patient for example it makes a connection with my user_id and the role_id in that table
I do not have a roles controller first of all, secondly in my User controller this is my code
class UsersController < ApplicationController before_filter :authenticate_user! load_and_authorize_resource def index @users = User.all end def show @user = User.find(params[:id]) end def update @user = User.find(params[:id]) current_user.update(user_params) end def user_params params[:user].permit( {:role_ids => []} ) end end
Here is the code generated in HTML from the checkboxes
<div class="form-group"><label for="user_roles">Roles</label><input id="user_role_ids_" name="user[role_ids][]" type="checkbox" value="1" /> admin<br /><input id="user_role_ids_" name="user[role_ids][]" type="checkbox" value="2" /> patient<br /><input id="user_role_ids_" name="user[role_ids][]" type="checkbox" value="3" /> doctor<br /></div>
Thanks in advance!