簡體   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