Как разбить запросы has_many на рельсы с помощью Kaminari - PullRequest
0 голосов
/ 02 июля 2018

метод индекса контроллера сообщений

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

index.html.erb

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

Я хочу разбить на страницы x.replies и пробовал все, но без толку

1 Ответ

0 голосов
/ 16 июля 2018

Отказ от ответственности:

В этом ответе я просто покажу, как вывести на экран ВСЕ ответы на пост (без нумерации страниц), так как я не использовал его некоторое время.

Кроме того, я использую формат haml, вы можете перевести его в формат erb онлайн

По вашему мнению для страницы индекса сообщений, добавьте это в каждую строку сообщения:

# 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"

В 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

обязательно добавьте "ответы" в контроллере сообщений в routes.rb

resources :posts do
  member do
    get :replies
  end
end

В 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

В 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}");

В 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

Пожалуйста, сообщите мне, если вы застряли в части или если у вас есть какие-либо вопросы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...