Home>

Always I am indebted.

As the title says, the model search results are narrowed down by the query parameter conditions on the controller side, and the model is passed to the view side and displayed.
The display uses kaminari, which is a paging gem, and if you press "more", 5 items will be displayed at a time.
It's been messed up, but in the end @items is in ActiveRecord format, which is the data you actually want to display.

There are 30 lines in the entire model, and if you narrow down to 10 with the controller, it is assumed that 5 will be displayed in the first display and the remaining 5 will be displayed when you press "more". If i keep it as it is, when you press View more, you will get 5 records on the 2nd page from the total 30 data.

When I stopped with pry in javascript, only page = 2 information was passed to the query parameter when "more" was pressed.

It is an automatic raw link of kaminari ↓
<% = link_to_next_page @items,'more', remote: true, id:'more_link'%>

How can I pass parameters such as search to?

It doesn't matter if it is a reference site, so I would appreciate it if you could teach me the appropriate method.
Thank you.

environment
Amazon Linux release 2 (Karoo)
Rails 5.2.3
Ruby version: 2.6.1

# xxxx.controller.rb
def show
    @items_tmp = []
    if! params [: jouken1] .nil?
      # Filtering process User.where (xxx: yyyy) etc.
    end
    if! params [: jouken2] .nil?
      # Filtering process User.where (xxx: yyyy) etc.
    end
・ ・
・ ・
・ ・
    #Revert array to Activereecord
    @items = @items_tmp .first.class.where (id: arr.map (&: id)) .page (params [: page]) .per (5) .order (created_at: "DESC")
   render "xxxxx/show"
end

# show.html.erb
<% = render'items'%>
<% = link_to_next_page @items,'more', remote: true, id:'more_link'%>

# _items.html.erb
<% @ items.each do | item |%>
<% item.id%>
<% item.name%>
  ・
・ ・
・ ・
<% end%>

# show.js.erb
$('#items'). append ("<% = escape_javascript (render'items', object: @items)%>");
$("# more_link"). replaceWith ("<% = escape_javascript (link_to_next_page (@items,'more', remote: true, id:'more_link'))%>");
<% # binding.pry%>
  • Answer # 1

    It was solved by adding a parameter in the following form to the argument of the method "link_to_next_page" that creates more view in view.

    show.html.erb
    <% = link_to_next_page @items,'more', remote: true, id:'more_link', params: params.permit!%>

  • Answer # 2

    For me

    Get all 10

    Hide the 6th and subsequent items with CSS on the view side

    If you press "more" in CSS or JavaScript, the 6th and subsequent items will be displayed.
    Do you want to implement it with

    ■ Addition
    You've already implemented Ajax for "more". I overlooked it.
    In the current situation, even if you get it with Ajax, render has been executed, so only 5 new ones are displayed.
    When using Ajax, it is necessary not to render.

    Try changing to execute the js file at the time of Ajax using format.js etc.