【问题标题】:Rails Polymorphing has_one association redirect_to controllerRails Polymorphing has_one 关联 redirect_to 控制器
【发布时间】:2018-11-02 14:03:24
【问题描述】:

在我的应用程序中,用户用于身份验证阶段,并且通过多态 has_one 关联,它将关联到不同类型的用户,具有不同的操作。

这是用户模型:

class User < ApplicationRecord
  has_secure_password
  validates :username, presence: true, 
     uniqueness: true
  belongs_to :role, :polymorphic => true, dependent: :destroy
end

这是相关的模型之一

class Guest < ApplicationRecord
  has_one :user, as: :role
end

日志和认证调用home_index_path,当前用户存储在current_user中。

在我的家庭控制器中:

def index
  if current_user
    redirect_to current_user.role
  else
    render 'unlogged'
  end
end

在 route.rb 我有:

resource :guest do
 member do
  get 'dashboard'
 end
end
resolve ('Guest') {[:guest]}

现在的问题:假设用户是 guest,这样我就被重定向到 GuestsController 的方法 show,但我需要重定向到方法 dashboard .

我该怎么办?

【问题讨论】:

  • 您能详细说明一下吗?你的问题现在还不清楚。
  • 好的,我会以另一种方式重新阐述。

标签: ruby-on-rails-5 polymorphic-associations


【解决方案1】:

好的,我已经用 polymorphing_path 解决了

redirect_to polymorphic_path([:dashboard, current_user.role])

【讨论】:

  • 记得将您自己的解决方案标记为“正确”以关闭问题:)
  • 我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
相关资源
最近更新 更多