0

In my Rails 4 app I have a number of static pages that should either be indexable by Google or not. I am using a variable indexable for this but there's probably a better way:

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?

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.

5
  • 1
    This is probably the wrong way to go about doing it. Many of these actions cannot be reached by GET. What about something akin to what rake routes does? It's not the action that's indexable, it's the path that ends up at that action, of which there could be many. Commented Jun 19, 2014 at 16:19
  • @tadman: OK, agreed. But how can I get those paths into an array? Commented Jun 19, 2014 at 16:39
  • 1
    @anthony has the beginning of a solution here. You might want to extend the routing system to allow for flags like indexable: true to be supported. Commented Jun 19, 2014 at 17:27
  • @tadman: OK, sounds good but I can't find anything on Rails' routes and "flags". Can you point me to an example? Commented Jun 19, 2014 at 18:03
  • You'd have to really wreck around in the Rails routing system, patching it to include options it normally doesn't have. This is probably massively over-complicating what should be a simple problem, though. A simple way is to use a particular style of comment in your routes.rb file you can find and process with a utility script, converting them into your map. Commented Jun 19, 2014 at 18:12

1 Answer 1

1

Maybe a before_filter would make sense?

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
Sign up to request clarification or add additional context in comments.

1 Comment

You'll have to trigger each action to discover if it's indexable, though. This is of dubious utility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.