【问题标题】:Devise super user设计超级用户
【发布时间】:2013-02-24 00:40:17
【问题描述】:

我使用 devise 进行注册,每个用户都属于一个位置。我想创建一个超级用户,无论创建多少位置,他都可以访问所有位置。

当前位置参数是 location_id = some integer

关于如何将 location_id 分配给等于所有位置的超级用户的任何想法?

我还应该补充一点,这样做的目的是让我的 iOS 应用能够验证和查看所有 api 数据。

我现在正在尝试定义 location_id = nil 以返回 location.all

if current_user.location = nil
    @locations = Location.all
  else
    @locations = current_user.location
  end

但我仍然需要定义 nil,因为使用具有 location_id = nil 的用户登录时出现以下错误

没有路线匹配 {:action=>"show", :controller=>"locations", :id=>nil}

【问题讨论】:

  • 前往位置#show 应该只提供一个位置。转到位置#index,而不是:/locations 而不是 /locations/nil
  • 谢谢蒂姆。转到位置#index 将不起作用,因为索引页面正在寻找 current_user.locations。我试图弄清楚如何定义 if @location = nil current_user.locations = Location.all
  • 它将转到current_user.locations,因为您告诉它转到current_user.locations。如果current_user.location = nil,没有什么能阻止你将它转到Location.all。强制#show 返回一个列表是错误的做法,而且只会让你很头疼。

标签: ruby-on-rails devise


【解决方案1】:

也许我误解了你正在做的更精细的细节,但是你可以在你的模型中添加一个 super_user 布尔字段,并在控制器中简单地使用这样的东西:

if current_user.super_user
  @locations = Location.all
else
  @locations = current_user.location
end

或者,您可以使用 nil 的 location_id 来表示“全部”,如果这样更适合您的需求,请使用 if current_user.location 而不是 if current_user.super_user

【讨论】:

  • 感谢这让我走上了正轨。我认为使用 nil 的 location_id 来表示“全部”效果最好,但想知道如何定义它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 2011-08-29
  • 1970-01-01
  • 2011-06-21
相关资源
最近更新 更多