ruby on rails - Saving the posted data from four random rows so they don't randomize after page generation -
i'm using code:
<h2> <% random_bullets = bullet.all %> <ul> <% random_bullets.shuffle.first(4).each |r| %> <br> <li><%= db.save(r.content) %></li> <% end %> </ul> </h2>
to pull 4 random bullets db , post them page; i'm trying them save bullets they're randomized after page generated. there can code achieve this? if not, can achieve this. code in embedded ruby , db sqlite3 database.
setting random bullets , randomising belongs in controller. once there recommend setting cookie contains array of bullet.ids in order. can iterate on array if exists else create new 1 , save it.
in controller:
if cookies[:bullets].nil? @bullets = bullet.all.shuffle.first(4) cookies[:bullets] = @bullets.collect(&:id) else @bullets = [] cookies[:bullets].each |id| @bullets << bullet.find(id) end end
in view:
<ul> <% @bullets.each |r| %> <li><%= r.content %></li> <% end %> </ul>
are aware it's invalid html put <ul>
within <h2>
or put <br>
within <ul>
?
Comments
Post a Comment