0

I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:

<%= link_to "Randomize", random_reader_path %>

But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.

The random method will do the same thing as index only provide a random page content @variable

Currently I have my reader Controller as

resources :reader

in my routes.rb

Ken W
  • 962
  • 4
  • 12
  • 19

1 Answers1

2

Add more RESTful actions!

resources :reader do
  get 'random', on: :collection
end

The route will be random_readers_path, though.

Buck Doyle
  • 6,333
  • 1
  • 22
  • 35
  • (I used the delicious Ruby 1.9 hash syntax; if you’re on tragic 1.8, you’ll have to write `get 'random', :on => :collection`.) – Buck Doyle May 12 '12 at 23:02
  • sadly I still have undefined local variable or method `random_readers_path' even after adding your exact code to my routes. (modified resources :reader, didn't add a new one ;) ) thanks for the link though – Ken W May 12 '12 at 23:06
  • by adding resources :readers do collection do get 'random' end end I get the path to work but when I click the link, I get uninitialized constant ReadersController – Ken W May 12 '12 at 23:11
  • Oh, oops. It’s traditional to use plural names on a `resources` call, I didn’t even grasp that you’re using `reader`. Any reason for that? It’ll be called `random_reader_index` because of that (no way to make `reader` any more singular), I think; you can run `rake routes` on the command line to see all the named routes. – Buck Doyle May 12 '12 at 23:13