1

我有一组具有公共属性的页面。我希望路由文件处理动态路由,但仅适用于公共页面。

我目前有以下,但没有限制,所有页面都是可见的。我想要的是只有在页面公开时才进入页面,否则会引发 404。

Page.public.each do |page|
  get "/:slug", controller: 'pages', action: 'show' if page.public?
end
4

2 回答 2

2

我会将这种行为放在控制器中而不是在 routes.rb 中,因为页面可能在运行时从私有更改为公共,并且生产中的路由在开始时仅初始化一次。

class PagesController

  before_filter :is_public, only => [:show]

  protected

    # Check if the page is public, otherwise raise a routing error.
    def is_public
      raise ActionController::RoutingError.new unless Page.find(params[:slug]).public?
    end
end
于 2013-02-18T04:04:40.813 回答
0

工作代码是(缺少“未找到”)

class PagesController

  before_filter :is_public, only => [:show]

  protected

  # Check if the page is public, otherwise raise a routing error.
  def is_public
    raise ActionController::RoutingError.new('Not Found') unless Page.find(params[:slug]).public?
  end
end
于 2013-02-23T16:53:55.433 回答