Ruby On Rails User Management Module Example
Create the tables
CREATE TABLE `roles` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 25 ) NOT NULL , `description` VARCHAR( 100 ) NOT NULL ) ENGINE = innodb; CREATE TABLE `users` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `user_name` VARCHAR( 50 ) NOT NULL , `password` VARCHAR( 15 ) NOT NULL , `first_name` VARCHAR( 50 ) NOT NULL , `last_name` VARCHAR( 50 ) NOT NULL , `age` INT( 3 ) NOT NULL , `email` VARCHAR( 25 ) NOT NULL , `country` VARCHAR( 20 ) NOT NULL , `role_id` INT NOT NULL, CONSTRAINT `fk_users_roles` FOREIGN KEY (`role_id`) REFERENCES `role`( `id`) ON DELETE CASCADE ) ENGINE = innodb; ALTER TABLE `tales` ADD `user_id` INT NOT NULL ; ALTER TABLE `tales` ADD FOREIGN KEY ( `user_id` ) REFERENCES `users` ( `id` ) ON DELETE CASCADE ;
Developing the User Management
ruby script/generate scaffold role role
Modifying the Model
# app/models/role.rb file class Role < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name has_many :users end
Customizing the Controller
# app/controllers/role_controller.rb def show @role = Role.find(params[:id]) @users=@role.users end
Refining the View
# app/views/role/show.rhtml <% for column in Role.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @role.send(column.name) %> </p> <% end %> <table> <tr> <th>UserName</th> <th>FirstName</th> <th>LastName</th> <th>email id</th> </tr> <% for user in @users %> <tr> <td><%=user.user_name%></td> <td><%=user.first_name%></td> <td><%=user.last_name%></td> <td><%=user.email%></td> </tr> <%end%> </table> <%= link_to 'Edit', :action => 'edit', :id => @role %> | <%= link_to 'Back', :action => 'list' %>
Developing the User Management Functionality
Generating the Scaffold
Modifying the Model
Providing Default Role to the User
Adding Display Action method to the Controller
Refining the View
Adding the Assign Action Method to the Controller
Generating the Scaffold
ruby script/generate scaffold user user
Modifying the Model
class User < ActiveRecord::Base validates_presence_of:user_name,:password,:first_name, :last_name, :age, :email, :country validates_uniqueness_of :user_name validates_numericality_of :age validates_format_of :email,:with =>/\A([^@\s]+)@((?:[-a-z0-\9]+\.)+[a-z]{2,})\Z/i belongs_to :role end
Assigning Default Role to a User
# app/controllers/user_controller.rb def create @user = User.new(params[:user]) @role=Role.find(:all, :conditions=>"name='Guest'") @user.role_id=@role.id if @user.save flash[:notice] = 'User was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end
Adding Display Action Method to the Controller
#app/controllers/user_controller.rb def display_assign @users = User.find(:all) @roles = Role.find(:all) end
Refining the View
#app/views/users/display_assign.rhtml <h1>Assign Role</h1> <% form_tag :action => 'assign' do %> <p><label for="user_user_name">User<br/> <select name="user"> <% @users.each do |user| %> <option value="<%= user.id %>"> <%= user.user_name %> </option> <% end %> </select> </p> <p><label for="role_role_name">Role<br/> <select name="role"> <% @roles.each do |role| %> <option value="<%= role.id %>"> <%= role.name %> </option> <% end %> </select> </p> <%= submit_tag "Assign Role" %> <% end %> <%= link_to 'Back', :action => 'list' %>
Adding the Assign Method to the Controller
#assign method in UserController def assign @user= User.find(params[:id]) if @user.upadate_attribute :role_id,params[:role] flash[:notice] = 'User was successfully assigned the role.' redirect_to :action => 'list' else flash[:notice] = 'Role could not be assigned to the selected user.' render :action=>'display_assign' end end
Testing the Functionalities
Uniqueness of the Role name
Validation of the User details during user registration/addition
Assigning of Role to a User
Displaying of Users assigned a particular Role