Skip to content

Commit 8a1d8da

Browse files
Added a 'Random Post' button to the homepage, created route and action to redirect to a random post
1 parent 5e1d8c2 commit 8a1d8da

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

app/controllers/notes_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,9 @@ def published_notes
501501
.where('node.status = 1')
502502
.where.not(nid: hidden_nids)
503503
end
504+
def random
505+
post = Post.order("RANDOM()").first
506+
redirect_to post_path(post)
507+
end
508+
504509
end

app/views/layouts/application.html.erb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,17 @@
121121
<%= render :partial => 'layouts/header' %>
122122
<div id="top_map"> </div>
123123
<div class="container">
124-
<div class="row">
125-
126-
<%= yield %>
127-
128-
</div><!--/row-->
129-
</div><!--/container-->
124+
<div class="row">
125+
126+
<div class="col-md-12 mb-2">
127+
<%= link_to '🎲 Random Post', random_path, class: 'btn btn-secondary' %>
128+
</div>
129+
130+
<%= yield %>
131+
132+
</div><!--/row-->
133+
</div><!--/container-->
134+
130135

131136
<%= render :partial => "layouts/footer" %>
132137

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
# Manually written API functions
1010
post 'comment/create/token/:id.:format', to: 'comment#create_by_token'
11+
get '/random', to: 'posts#random'
12+
1113

1214
post '/node/update/title' => 'notes#update_title'
1315

0 commit comments

Comments
 (0)