ruby - Using constraints in Rails to change the default root_path for some users -
i looking way change default routing (root_path specifically) of app using other simple if/then check different types of users. found this code example online, can't make work app , main problem don't understand underlying code does, therefore cannot adapt code app.
first create rule in router:
root 'admin#index', constraints: roleconstraint.new(:admin)
then create new file called role_constrait.rb in lib directory , user code:
class roleconstraint def initialize(*roles) @roles = roles end def matches?(request) @roles.include? request.env['warden'].user.try(:role) end end
i see guy using warden here, using cancan, , since piece of code makes no sense me, can't make changes it, have tried, keep getting undefined local variable or method `root_path' error.
would highly appreciate help!
i don't think router should in charge of doing that. it's hard implement , hard test. more than, it's extremely inconsistent - different users see different things under same url. normally, people wrap admin area /admin
scope.
so, suggestion routing straightforward (just /admin
scope), , put authentication logic controllers (e.g. admincontroller
).
if need further advice, ask.
Comments
Post a Comment