Im testing ruby on rails and to practice i set my mind on making a login
system. because i would come across of regularly made source.
files:
user_controller.rb, user.rb
my table layout:
users: |id|username|password
usertypes: |id|name| (admin, moderator, guest)
users_usertypes: |usertype_id|user_id| (+foreign key assocc)
i have this in my user.rb file:
class User < ActiveRecord::Base
has_and_belongs_to_many :usertypes, :join_table =>“users_usertypes”
end
class Usertype < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table =>“users_usertypes”
end
in the user_controller (or application controller) are methods like this
(sadly i cant gettem to work)
this method should verify if the active user has an ‘admin’ usertype
connected to it
def auth_by_type(type)
@user = User.find(@session[:user_id]) #this searches for current user
if @user.usertype == type
flash[:notice] = “You are authorized as an #{type}”
else
flash[:notice] = “You are not authorized to view this page…”
redirect_to :action=>:login
end
end
def create
#insert a new usertype in the users_usertypes table
end
def update
#update a usertype in the users_usertypes table
end
how can i use a method with an attribute such as auth_by_type in a
before_filter. I tried:
before_filter :authorize_by_type(“admin”)
but that doesnt work…
Ive been spending hours and hours on this simple thing but i cant fix it.
arghh
I have a couple of questions.
- How can i update/set the usertype of a user
- How can i see if a user has a usertype ‘admin’ name
- How can i use the method from 2. in a before_filter