繁体   English   中英

如何在Ruby on Rails中生成控制器动作数组?

[英]How to generate an array of controller actions in Ruby on Rails?

在我的Rails 4应用程序中,我有许多静态pages ,它们应该可以被Google索引或不能被索引。 我为此使用了一个indexable的变量,但是可能有更好的方法:

class PagesController < ApplicationController

  def home
    indexable = true
  end

  def about_us
    indexable = true
  end

  def secret_stuff
    indexable = false
  end

end

如何生成所有indexable页面的数组?

我尝试在帮助器中执行此操作,但是它不起作用:

def indexable_pages
  array = []
  PagesController.instance_methods(false).each do |action|
    if action.indexable == true # this won't work of course
      array << action
    end
  end
  array
end

谢谢你的帮助。

也许before_filter会有意义?

class PagesController < ApplicationController
  before_filter :set_indexable, except: [:secret_stuff]

  def home
  end

  def about_us
  end

  def secret_stuff
  end

  private 

  def set_indexable
    @indexable = true
  end

end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM