It would be exactly like Wikipedia's "Random Article" button. Takes you to a random webpage on the site.
Asked
Active
Viewed 57 times
-2
-
I would do something like that: http://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql?answertab=votes#tab-top – Oct 30 '15 at 23:47
2 Answers
0
<%= button_to "Random Article", article_path(random_article) %>
#app/helpers/application_helper.rb
class ApplicationHelper
def random_article
Article.order("RAND()").first.pluck(:id)
end
end
With help from this article: Random record in ActiveRecord

Community
- 1
- 1

Richard Peck
- 76,116
- 9
- 93
- 147
-1
Some more information would make this much easier to answer, but there are a few ways to do it. The best, in my opinion, would be something like this:
routes.rb:
resources :articles do
get :random_article
end
#OR
get 'random_article' => 'some_controller#random_article', as: 'random_article'
articles_controller.rb
def random_article
redirect_to article_path(Article.all.sample.id)
end
And on your page:
<%= link_to 'Random Article', random_article_path %>

Rockster160
- 1,579
- 1
- 15
- 30