Ruby on rails 3.1 Rails允许访问一个页面而不限制整个控制器

Ruby on rails 3.1 Rails允许访问一个页面而不限制整个控制器,ruby-on-rails-3.1,Ruby On Rails 3.1,因此,我有3种类型的用户: admin moderator regular user 我用控制器范围的系统锁定了版主和管理员页面,如下所示: 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

因此,我有3种类型的用户:

admin
moderator
regular user
我用控制器范围的系统锁定了版主和管理员页面,如下所示:

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
但是我需要让普通用户访问多个控制器的编辑页面(当然还有更新操作)。但只需编辑和更新

如果我这样做:

before_filter :authorize, :except => :edit
然后任何人(即使未登录)都可以访问这些页面

我该怎么做那样的事呢

编辑

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

  def update_successful
    skip_before_filter :authorize
  end
在普通用户编辑条目后,能够为更新成功页面提供服务。但是我得到了这个错误:

undefined method `skip_before_filter' for #<HomeController:0x007ff782aeb6f0>
未定义的方法“在过滤器之前跳过”#

您可以明确跳过任何全局应用过滤器:

skip_before_filter :authorize, :only => [:edit, :update]
或者首先不要将其应用于相关行动:

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

要回答进一步的问题:请将此添加到应用程序控制器:

def upload_successful
end
这是一个空方法,它显式定义了Rails在呈现
home/upload\u successful.html.haml
模板时隐式使用的操作。然后,通过修改筛选器从该方法中删除身份验证:

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

这是Rails中的一个好方法-它有助于理解默认情况下的渲染,这是您的
上载\u成功的
模板在没有定义匹配的控制器或操作的情况下显示的内容。

您当前如何使用这两种方法?作为过滤器之前的
?但通过这种方式,任何人都可以在没有帐户的情况下查看这些页面。我按照你的建议做了,还向每个具有编辑和更新方法的控制器添加了一个
,在\u filter:authorize\u user
(检查普通用户是否已登录)之前添加了一个
。不确定这是否是正确的方法,仍然存在一些问题。此外,我有一个页面,我希望普通用户在成功更新后可以重定向到该页面。我尝试在视图(haml模板)中放置:
skip\u-before\u-filter:authorize
,但这似乎不起作用。它只是一个普通页面,位于“home”目录中,没有控制器。我怎样才能让他们访问该页面?(感谢您的回复,顺便说一句)。过滤器在控制器上运行,每个操作都有一个过滤器-如果没有明确显示,则默认的
ApplicationController
。只需将一个(空)方法添加到具有页面名称的方法中,并为此设置筛选器。很抱歉,Thilo,我不明白“只需将一个(空)方法添加到具有页面名称的方法中,并为此设置筛选器”是什么意思。在此上下文中“那”是什么?您的
ApplicationController
类。