【问题标题】:Adding a new scope to Devise为设计添加新范围
【发布时间】:2017-09-18 02:34:18
【问题描述】:
我的目标是创建一个只能由具有特定role 的用户访问的范围,在本例中为“编辑器”。或者更确切地说是由管理员和编辑。
我正在使用设计。这是我尝试的一部分:
scope "/editor" do
devise_for :editors
# ...other routes
end
通过“rails server”运行时:
/home/user123/.gem/ruby/2.4.1/gems/activesupport-5.1.3/lib/active_support/inflector/methods.rb:269:in `const_get': uninitialized constant Editor (NameError)
我应该如何解决它?
【问题讨论】:
标签:
ruby-on-rails
ruby
devise
【解决方案1】:
我认为您误解了 scope 参数的使用。如果您想在某个视图中限制访问,您可以执行以下操作。假设您有一个视频流服务,普通用户只能访问索引并显示操作/视图,而管理员可以看到其余部分(newedit 等...)。
您在app/controllers/concerns/ 提出问题(google DRY)并将其命名为 admin.rb
module IsAdmin
extend ActiveSupport::Concern
def admin?
return redirect_to videos_new, alert: "Access Denied" unless current_user.admin?
end
end
现在在您的 videos_controller.rb 中包含我们刚刚提出的问题并告诉控制器您想在哪些视图中使用管理员?方法。
Class VideosController < Applicationcontroller
include IsAdmin
before_action :admin?, only: [:new, :edit]
end