I am trying to redirect depending on a value in the database.
for example if a technician is logged in they should be redirected to site A else everyone else should be redirected to site B.
my attempt so far looks as follows
def create
@user_session = UserSession.new(params[:user_session])
@user = User.new(params[User.where(:username => @user_session.username)])
#@user = UserUser.where(:username => @user_session.username)
#@user = User.select("tech").find(@user)
#@user = User.new
respond_to do |format|
if @user_session.save
if @user.tech == 1
format.html { redirect_to '/tech/index', notice: 'Login Successful A Company' }
else
format.html { redirect_to :users, notice: @user.tech }
end
else
format.html { render action: "new" }
end
end
end
The problem is that the if statement always resolves to the code in the else statement instead of the code above the else statement. In this case the @user.tech is nil I am somehow not pointing at the data in the database
Ok
Try changing this line
Here you do something strange, and I think you should get an error message. @user is just a new instance of the User model in memory
I think you need this
But, if the user name is not unique, this is a security problem. A admin can have the same name as a user So you should do something like
tech? method in user.rb ( the model )
I am guessing tech either 1 or 0 in the database?
Now you can do
And it will return ture or false