9 Matching Annotations
- Apr 2021
-
github.com github.com
-
# unauthenticated do # as :user do # root to: 'devise/registrations#new' # end # end
-
# authenticated :user, lambda {|u| u.role == "admin"} do # root to: "admin/dashboard#show", as: :user_root # end
-
-
github.com github.com
-
class LoggedInConstraint def self.matches?(request) request.env['warden'].authenticate? end end
(have not tried)
-
-
stackoverflow.com stackoverflow.com
-
class AuthConstraint def initialize(&block) @block = block || ->(_) { true } end def matches?(req) user = current_user(req) user.present? && @block.call(user) end def current_user(req) User.find_by_id(session[:user_id]) end end This is a flexible approach to defining route access based on any desired variable (roles, auth, etc...)
Good solution, and might be needed if you want to base routes on roles, etc. — but this one is even easier if all you need is for it to be conditional based on signed in or not (because devise provides authenticated helper):
-
scope module: 'authenticated', constraints: AuthConstraint.new { |user| user.present? } do # Management dashboard root 'dashboards#index' end root 'home#index'
-
-
stackoverflow.com stackoverflow.com
-
authenticated :user do root 'calendars#index', as: :authenticated_root end unauthenticated :user do root 'pages#home', as: :unauthenticated_root end
-
-
guides.rubyonrails.org guides.rubyonrails.org
-
You can also specify constraints as a lambda:
-
- Nov 2020
-
-
In short, I think the idea of public methods being actions works just fine as an api.
-
-
guides.rubyonrails.org guides.rubyonrails.org
-
You can use the match method with the :via option to match multiple verbs at once:
-