authentication - Ruby On Rails - Setting Object Values -
i've created ruby on rails application in users can login , logout, , new accounts can created. users have integer "rankid", , depending rankid have different permissions on site.
i want users able upgrade next rank going rooturl/upgrade
- in routes.rb have following:
map.connect '/upgrade', :controller => 'users', :action => 'upgrade'
which makes use of following method in users controller:
def upgrade @currentid = session[:user_id] @user = user.find(@currentid) if @user.rankid = 0 @user.rankid = 1 redirect_to root_url, :notice => "upgraded vip!" return end if @user.rankid = 1 @user.rankid = 2 redirect_to root_url, :notice => "upgraded admin!" return end end
i setup authentication using this tutorial , can't figure out why wont work. sorry if stupid mistake - i'm new both ruby , rails.
first, if
statements need double equals sign, compare @user.rankid
0
instead of setting 0
.
if @user.rankid == 0
next, you're never saving users after updating them. lastly, use elsif
on second block. otherwise, user upgraded vip , upgraded admin. using else/elsif, don't need hard code return statement.
full code:
def upgrade @user = user.find(session[:user_id]) if @user.rankid == 0 @user.update_attributes(:rankid => 1) redirect_to root_url, :notice => "upgraded vip!") elsif @user.rankid == 1 @user.update_attributes(:rankid => 2) redirect_to root_url, :notice => "upgraded admin!" end end
Comments
Post a Comment