-4

I need to implement pagination for the following hash. I need to show the rooms floor wise with pagination.

Hash = { floor_1 : [101,102,103], floor_2: [201,203,204], floor_3: [301,302,303] }

Is there any idea on how to do that? I am using will pagination gem and it is not working.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • That is not valid Ruby code. – sawa Aug 13 '18 at 07:17
  • Just a guess, but I think it is `{ <#Floor ...> => [101, 102, 103], ... }` An object of `Floor`, although nothing provided by OP. – Jagdeep Singh Aug 13 '18 at 07:18
  • 1
    Also, it is a very bad idea to redefine `Hash`, which is a very basic plain Ruby class. – sawa Aug 13 '18 at 07:19
  • "it is not working" is not a precise enough error description for us to help you. *What* doesn't work? *How* doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? – Jörg W Mittag Aug 14 '18 at 12:50

1 Answers1

0

Do you mean you want to show the values relating to each floor_x key on a different page?

If so, at a very basic level, you could use the following (for example, in your controller):

def index
  @hash = { floor_1: [101,102,103], floor_2: [201,203,204], floor_3: [301,302,303] }
end

In your view:

<% params[:floor] ||= 1 %> # show values for floor_1 if param not provided
<% hash["floor_#{params[:floor]".to_sym].each do |vals| %>
  # display your values
<% end %>
<%= link_to 'Down A Floor', your_path(params[:floor].to_i - 1) if hash["floor_#{params[:floor].to_i - 1}".to_sym] %>
<%= link_to 'Up A Floor', your_path(params[:floor].to_i + 1) if hash["floor_#{params[:floor].to_i + 1}".to_sym] %>

Extremely basic but might get you on the right track. Basically, you make the hash of floors / values available to a view, and display the current floor with links to the floor above and below if they exist.

Very unlikely you'd want it looking like that in your final code, lots of nasty inline logic, though I wanted to keep things very simple. Have look into using a presenter or decorator to handle this.

SRack
  • 11,495
  • 5
  • 47
  • 60