Better data structure and design for this feature.

In my Rails application, I have the Situations section, which is basically a textual description of the situation. But there are several of them. I want to be able to display only one at a time and each on my page (first created first), and then at the bottom of the page I have links that go to "Old" and New situations.

Suppose there is an object in my code @situationsthat contains all the situations that I want to display. What to do next (in the controller and in the view).

+3
source share
2 answers

I would use pagination ( will_paginate) and set the number of elements per page to 1.

Then you can use current_pageand next_pageto make your links. See source here.

Another thing is that it will be just a standard action with the changes necessary for will_paginate.

This screencast should give you a good idea of ​​what you need, but remember that there have been some changes in the plugin since its inception. Details are on the will_paginategithub wiki.

+3
source

models / situation.rb

class Situation < ActiveRecord::Base
  default_scope :order => 'created_at desc'
end

Controllers / situations_controller.rb

class SituationsController < ActionController::Base
  def index
    @situations = Situation.all
  end
end

view / situation / index.html.erb

<h1>Situations</h1>
<%= render @situations %>

view / situation / _situation.html.erb

<h2><%= situation.name -%></h2>
<p><%= situation.description -%></p>
+1
source

Source: https://habr.com/ru/post/1705379/


All Articles