Search code examples
ruby-on-railsrubyfriendly-urlfriendly-id

Root with slug rails 4 routing


Currently I'm using friendlyID gem and I just want to redirect my application to a root with slug if the user is authenticaated.

url

http://localhost:3000/companyABC/dashboard

Here's my current code

  authenticated :employer do
    root to: redirect("/:slug/dashboard"), as: :employer_root
  end

  namespace :company, path: "/:slug" do
    resources :dashboard
    namespace :settings do
      resources :collaborators
    end
  end

but the problem is when I sign in it redirects me to http://localhost:3000/:slug/dashboard


Solution

  • As far as I know, you can't use lambdas in routes, so it's impossible to access DB and fetch company's slug in routes.

    But if you use devise, you can overwrite method #after_sign_in_path_for, so user will be redirected not to root_url but to specified url instead. For example:

    class ApplicationController < ActionController::Base
      def after_sign_in_path_for(user)
        company_dashboards_url(user.company)
      end
    end
    

    More information here: devise docs about after_sign_in_path_for