I’ve created a Ruby on Rails application in which users can login and logout, and new accounts can be created. Users have an integer “rankid”, and depending their rankid have different permissions on the site.
I want users to be able to upgrade to the next rank by going to ROOTURL/upgrade – so in my routes.rb I have the following:
map.connect '/upgrade', :controller => 'users', :action => 'upgrade'
Which makes use of the following method in my 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 to VIP!"
return
end
if @user.rankid = 1
@user.rankid = 2
redirect_to root_url, :notice => "Upgraded to Admin!"
return
end
end
I setup the authentication using this tutorial and can’t figure out why it wont work.
Sorry if this is a really stupid mistake – I’m very new to both Ruby and Rails.
First, your
ifstatements need a double equals sign, which will compare@user.rankidto0instead of setting it to0.Next, you’re never saving your users after updating them. Lastly, use an
elsifon your second block. Otherwise, a user will be upgraded to VIP and then immediately upgraded to admin. By using an else/elsif, you don’t need to hard code a return statement.Full code: