【问题标题】:I want to set authenticated users' root to a nested resource, their dashboard, instead of the home#index page non-authenticated users use as root我想将经过身份验证的用户的根设置为嵌套资源,即他们的仪表板,而不是未经过身份验证的用户用作根的 home#index 页面
【发布时间】:2013-07-02 13:34:04
【问题描述】:

如果我按原样使用以下代码 (dashboards#show),我会收到一条无法找到记录的错误 Couldn't find Dashboard without an ID,因为路径中没有用户 ID(我通常使用以下链接到此仪表板: user_dashboard_path(current_user, current_user.dashboard)

我想我需要将用户传递到设置根路径的代码块中,但我不确定如何实现。

有什么建议吗?

这是我的整个 routes.rb 文件:

MyApp::Application.routes.draw do
  resources :users do
    resources :dashboards, only: [:show, :edit, :update, :destroy]
  end

  authenticated :user do
    # Instead of the show action, I want to go to user_dashboard which is
    # GET    /users/:user_id/dashboards/:id(.:format)      dashboards#show
    root :to => "dashboards#show", as: :authenticated_root
  end

  root :to => "home#index"
  devise_for :users, :controllers => { :registrations => :registrations }

end

【问题讨论】:

    标签: ruby-on-rails devise routing ruby-on-rails-4


    【解决方案1】:

    选项 1:
    在您的仪表板控制器中,更改为:

    @user = current_user
    @dashboard = current_user.dashboard
    

    这也将阻止用户查看其他用户的仪表板。

    选项 2:
    现在,如果用户可以正常查看其他用户的仪表板,则更改您的路线:

    root :to => "dashboards#find", as: :authenticated_root
    

    在您的仪表板控制器中:

    def find
      redirect_to user_dashboard_path(current_user, current_user.dashboard)
    end
    

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 2016-01-17
      • 2018-01-07
      • 2017-09-11
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2019-09-25
      相关资源
      最近更新 更多