ruby-on-rails-3.1 - Rails 允许访问一页而不限制整个 Controller

标签 ruby-on-rails-3.1

所以我有 3 种类型的用户:

admin
moderator
regular user

我使用 Controller 范围的系统锁定了主持人和管理页面,如下所示:

def authorize
  unless User.find_by_id(session[:user_id]) and User.find_by_id(session[:user_id]).moderator == true
    redirect_to login_url, :notice => "Please log in with a moderator account"
  end
end

def authorize_admin
  unless User.find_by_id(session[:user_id]) and User.find_by_id(session[:user_id]).admin == 1
    redirect_to login_url, :notice => "Only admins can access the page you tried to access"
  end
end

但我需要授予普通用户访问多个 Controller 的编辑页面(当然还有更新操作)的权限。但只是编辑和更新。

如果我这样做:

before_filter :authorize, :except => :edit

然后任何人(即使未登录)都可以访问这些页面。

我该如何去做这样的事情?

编辑

根据 Thilo 的建议,我将以下内容添加到 application_controller.erb 文件中:

  def update_successful
    skip_before_filter :authorize
  end

能够在普通用户编辑条目后提供 update_successful 页面。但是我收到此错误:

undefined method `skip_before_filter' for #<HomeController:0x007ff782aeb6f0>

最佳答案

您可以显式跳过任何全局应用的过滤器:

skip_before_filter :authorize, :only => [:edit, :update]

或者首先不要将其应用于相关操作:

before_filter :authorize, :except => [:edit, :update]

编辑

要回答您的进一步问题:将其添加到您的应用程序 Controller 中:

def upload_successful
end

这是一个空方法,它显式定义了 Rails 在渲染 home/upload_successful.html.haml 模板时隐式使用的操作。然后,通过修改过滤器从该方法中删除身份验证:

before_filter :authorize, :except => [:upload_successful]

这是一个很好的introduction to rendering在 Rails 中 - 它有助于理解默认渲染,这是您的 upload_successful 模板在没有定义匹配 Controller 或操作的情况下显示的内容。

关于ruby-on-rails-3.1 - Rails 允许访问一页而不限制整个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10231313/

相关文章:

ruby-on-rails - Rails 3.1 一种模型的不同 View

ruby-on-rails - 在 Rails View 问题中复数

ruby-on-rails - 'merge' :String - Rails 3. 1 的未定义方法 "test"

ruby-on-rails - Rails 3.1 Assets 管道 : Ignore backup files from Emacs and other editors

ruby-on-rails - 如何创建具有多种类型的 has_many 关联?

ruby-on-rails-3.1 - 帖子中没有方法错误#new

javascript - Rails 3.1 - 调用 AJAX 请求两次

ruby-on-rails - 如何在 rails 3.1 中加载样式表

ruby-on-rails - 是否可以在不使用 respond_to 方法和使用非常规文件名的情况下呈现 js.erb?

ruby-on-rails - sql执行后得​​到切片结果