0

posts controller index method

@posts = Post.includes(:replies).all

index.html.erb

 <% @post.each do |x| %>
    <%= x.content %>
    <% x.replies.each do |s| %>
    <%= s.content %>
    <% end %>
    <% end %>

I want to paginate x.replies and tried everything but with no use

masnion
  • 45
  • 7
  • to begin with tracking down the cause, look at this https://stackoverflow.com/questions/6543248/rails-pagination-with-kaminari-with-has-many-through-relationship, maybe you can check in console first if `page` works with the children – Jeffrey M Castro Jul 03 '18 at 07:45
  • yes it works when ever I type `Post.find(1).comments.page(1)` I get results ... but is there a way to do it outside the console :) – masnion Jul 03 '18 at 22:26
  • what page do you want to display, u want a list of posts page (index page for posts), then in each post ROW (im assuming this is a table display), u want to list the all the replies, paginated, in that same row? – Jeffrey M Castro Jul 04 '18 at 01:41
  • yes i a, trying yo display posts index paginated and each post has it's comments paginated inside so that's 2 pagination – masnion Jul 11 '18 at 17:13
  • hmm this is quite challenging because you are iterating on each post, so you cannot just paginate in the view directly. maybe a better way to do this is to have a remote link instead, that when you click, it will open a modal pop-up and display all comments of that post. when you click the link it will request from the controller given the `post id`, you will get all of its comments and just render that in the modal. do you know already how to do this? – Jeffrey M Castro Jul 12 '18 at 06:02
  • well ,, I am relatively new to rails so I understand what are you saying but missing some points ho to implement it so kindly if you could do this this will answer my question ... – @JeffreyMCastro – masnion Jul 14 '18 at 21:31
  • happy to help, i am not with m computer now so at the moment, you can start to study/google “link_to remote true” and “link_to modal”. i will make a demo for you when i get back home – Jeffrey M Castro Jul 15 '18 at 01:28
  • I know remote true so will study link to modal thanks and drive safe :) – masnion Jul 15 '18 at 01:42

1 Answers1

0

Disclaimer:

In this answer I will just show how to pop-up ALL the replies of a post (no pagination) since I have not been using it for a while.

Also, i am using haml format, you can translate it to erb format online

In your view for posts index page, add this to every row of post:

# link to remote true. will call the "replies" action in the posts controller
= link_to('Show post replies', replies_post_path(post), remote: true, data: { toggle: 'modal', target: '#post_replies_modal' })
# this should be present in the view as well, this is the base for the modal pop-up which is hidden by default, and will be shown once the link_to above is clicked.
= render partial: "posts/post_replies_modal"

In app/views/posts/_post_replies_modal.html.haml

# this is the modal content. right now it only shows the title 'List of replies to the post' and the buttons to close the modal. Nothing else.
# the content will be populated by the javascript request (remote: true part of the link_to)
.modal.inmodal.fade{ "id": "post_replies_modal", "tabindex": "-1", "role": "dialog" }
  .modal-dialog
    .modal-content
      .modal-header
        %button.close{ "data-dismiss": "modal", type: "button" }
          %span{ "aria-hidden": "true" } ×
          %span.sr-only Close
        %h4.modal-title
          List of replies to the post
      .modal-body
        #post_replies_content
      .modal-footer{ style: "margin-top: 0 !important;" }
        %button.btn.btn-white{"data-dismiss" => "modal", :type => "button"} Close

make sure to add "replies" in the posts controller in routes.rb

resources :posts do
  member do
    get :replies
  end
end

In app/controllers/posts_controller.rb

# this is the part that will populate the body of the modal pop-up
# this will query the replies pertaining to the post
def replies
  # paginate post_replies here
  # post_replies = Post.find(params[:id]).replies

  respond_to do |format|
    format.js do
      # render template part means it will use "app/views/posts/replies.js" since 'js' is our request format
      render template: "posts/replies", locals: { post_replies: post_replies }
    end
  end
end

In app/views/posts/replies.js.haml

# this is what is called by the 'render template' from the controller
# it just uses a partial to generate an html for the replies table
# and then inserts that table in the modal body, which is in the #post_replies_content id in our popup
- post_replies_table_partial = render(partial: 'post/replies_table', format: 'html', locals: { post_replies: post_replies })

$("#post_replies_modal #post_replies_content").html("#{escape_javascript post_replies_table_partial.html_safe}");

In app/views/posts/_replies_table.html.haml

# this is the partial called from above, which generates the html code for displaying the replies table
%table.table.table-striped.table-bordered
  %thead
    %tr
      %th Reply ID
      %th Reply message
  %tbody
    - post_replies.each do |pr|
      %tr
        %td= pr.id
        %td= pr.message

Kindly inform me if you get stuck in a part or if you have any questions.

Jeffrey M Castro
  • 367
  • 3
  • 12