简体   繁体   English

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

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

In my Rails 4 app I have a number of static pages that should either be indexable by Google or not. 在我的Rails 4应用程序中,我有许多静态pages ,它们应该可以被Google索引或不能被索引。 I am using a variable indexable for this but there's probably a better way: 我为此使用了一个indexable的变量,但是可能有更好的方法:

class PagesController < ApplicationController

  def home
    indexable = true
  end

  def about_us
    indexable = true
  end

  def secret_stuff
    indexable = false
  end

end

How can I generate an array of all the pages that are indexable ? 如何生成所有indexable页面的数组?

I tried doing this in a helper but it's not working: 我尝试在帮助器中执行此操作,但是它不起作用:

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

Thanks for any help. 谢谢你的帮助。

Maybe a before_filter would make sense? 也许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