0

Here is my code.

Controller:

def bisac_main_counts
  q = "
  MATCH (b:Bisac) WHERE (b.bisac_code =~ '.*000000')
  WITH b, size((b)<-[:INCLUDED_IN]-()) as wokas_count
  RETURN b.bisac_code as bisac_code, b.bisac_value as bisac_value, wokas_count
  ORDER BY b.bisac_code
  ;"
  @result = Neo4j::Session.current.query(q).to_a
  if params[:limit]
    @result = Kaminari.paginate_array(@result).page(params[:page]).per(params[:limit])
  else
    @result = Kaminari.paginate_array(@result).page(params[:page])
  end
end

View:

<%= render 'home/main_links' %>
<%= paginate @result %>
<%= render 'bisac_counts' %>
<%= form_tag bisac_main_counts_path, method: :get do %>
   <%= select_tag :limit, options_for_select([5, 10, 15, 20, 25], selected: params[:limit] || 25) %>
<% end %>

Partial:

<table>
  <thead>
    <tr>
      <th>Bisac Code</th>
      <th>Bisac Value</th>
      <th>Wokas Count</th>
    </tr>
  </thead>

  <tbody>
    <% @result.each do |record| %>
      <tr>
        <td><%= record.bisac_code %></td>
        <td><%= record.bisac_value%></td>
        <td><%= record.wokas_count %></td>
      </tr>
    <% end %>
  </tbody>
</table>

Apparently params[:limit] is not set in the view. It is always null. Changing pages is working.

What I am doing wrong here?

Shadwell
  • 34,314
  • 14
  • 94
  • 99
LDB
  • 692
  • 6
  • 18
  • Possibly cast your param to a number? `@result = Kaminari.paginate_array(@result).page(params[:page]).per(params[:limit].to_i)` – panmari Aug 10 '15 at 19:40
  • Changed this line (in the view) like this below in order to use a cast, no difference: <%= select_tag :limit, options_for_select([5, 10, 15, 20, 25], selected: params[:limit].to_i || 10) %> – LDB Aug 10 '15 at 19:44
  • What's the params hash of your request? – panmari Aug 10 '15 at 19:47
  • Could you update with the paginated query that is appearing in the logs? – Brian Underwood Aug 10 '15 at 19:50
  • Never mind, please see below, I was missing a form to be used to change the limit param and a way to trigger it. Please see the code for the view and how was this changed. – LDB Aug 10 '15 at 19:53
  • The cypher query from log: Started GET "/bisac_main_counts" for 127.0.0.1 Processing by BisacsController#bisac_main_counts as HTML CYPHER 800ms MATCH (b:Bisac) WHERE (b.bisac_code =~ '.*000000') WITH b, size((b)<-[:INCLUDED_IN]-()) as wokas_count RETURN b.bisac_code as bisac_code, b.bisac_value as bisac_value, wokas_count ORDER BY b.bisac_code ; Rendered home/_main_links.html.erb (0.2ms) Rendered bisacs/_bisac_counts.html.erb (0.2ms) Rendered bisacs/bisac_main_counts.html.erb within layouts/application (14.2ms) Completed 200 OK in 864ms (Views: 63.1ms) – LDB Aug 10 '15 at 20:35

1 Answers1

3

What was missing was a form for changing the limit and a way to trigger it. Here are the changes (in the view only):

<%= render 'home/main_links' %>
<%= paginate @result %>
<%= render 'bisac_counts' %>

<%= form_tag bisac_main_counts_path, method: :get, id: 'limit_form' do %>
   <%= select_tag :limit, options_for_select([5, 10, 15, 20, 25], selected: params[:limit] || 10) %>
<% end %>

<script type="text/javascript" >
  $(function(){
    $('#limit').change(
      function() {
       $('#limit_form').submit();
      });
  });
</script>
LDB
  • 692
  • 6
  • 18