动态根Ruby on Rails

问题描述 投票:1回答:1

您好,我有一个软件,用户可以在其中拥有许多项目,在这些项目中,他们可以拥有一个“最喜欢的”项目,只要他们有一个项目,就会在他们登录时加载。

无论他们是否有喜欢的项目,我都希望路径为“app.example.io”。如果他们没有项目,我希望路径为“app.example.io”,但将它们发送到“projects / index”,如果他们确实有一个“最喜欢的”项目,我希望路径保持相同的“应用程序” .example.io“但我想将它们发送到特定项目”projects /:project_id“。

点是我想要一种方法让网址在登录时总是相同,无论他们是否有“喜欢的”项目。

  constraints subdomain: 'app' do
    namespace :users do

        devise_for :users, :skip => [:registrations], controller: {passwords: 'passwords', sessions: 'sessions'}, path: '', path_names: {sign_in: 'login', sign_out: 'logout'}

         authenticated do
          root to: "projects#show", as: :authenticated
        end

        unauthenticated do
          root to: "users/sessions#new", as: :unauthenticated
        end
      end

      devise_scope :user do
        root to: "projects#show"
      end

这是我的路由文件,我的会话控制器是:

class Users::SessionsController < Devise::SessionsController
  layout "login"

  def after_sign_in_path_for(resource)    
    if !current_users_user.project_id
    "/projects"
    else
    "/projects/#{current_users_user.project_id}"
    end 
  end

  def after_sign_out_path_for(resource)     
    "/users/login"
  end 
end

感谢任何帮助,谢谢!

ruby-on-rails ruby
1个回答
0
投票

您可以尝试如下

"/projects/#{current_users_user.projects[0].project_id}" #=> projects is the table name you can replace which you have

或者您需要尝试验证项目控制器

before_action :authenticate_user!

然后在登录后重定向到项目索引路径,其中当前用户的所有项目只能显示当前用户项目

我认为会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.