Reputation: 4907
I'm pretty sure that this is a simple fix but I'm not seeing it. I have an app where I'd like to show a music_videos comments. Below are my controllers:
def show
@music_video = MusicVideo.find(params[:id])
@comment = Comment.new
@comments = @music_video.comments.page(params[:page]).per(3)
end
The above is my music video controller.
def create
@music_video = MusicVideo.find(params[:music_video_id])
@comment = @music_video.comments.build(comment_params)
if @comment.save
flash[:notice] = "Comment Submitted"
redirect_to music_video_path(@music_video)
else
render 'music_videos/show'
end
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to root_path, notice: "Comment Deleted"
end
private
def comment_params
params.require(:comment).permit(:body)
end
Above is my comments controller
Finally my show page:
<div class="comments_row">
<% @music_video.comments.each do |comment| %>
<% if user_signed_in? && current_user.admin? %>
<p class="comment"><%= comment.body %></p>
<%= link_to 'Delete Comment', music_video_comment_path(@music_video,comment),
method: :delete %>
<% else %>
<p class="comment"><%= comment.body %></p>
<% end %>
<%end%>
</div>
<%= paginate @comments %>
I'm pretty sure something is wrong with my controllers, but I'm not sure exactly what it is. @comments is in the correct controller (MusicVideo) within the correct CRUD operation (show). Currently I have six comments in a particular show page and the pagination shows up just fine but the six comments are not paginated. Any thoughts?
EDIT-------------
I figured out one the problem but stumbled on a new one. I figured out that in my controller I am declaring @comments = pagination etc. etc. when in my views there is no @comments to paginate. The problem is now that when I use
<%= paginate @comment %>
the code will break.the problem now that I'm having is what variable to paginate. Trying this code will also break
<%= paginate @music_video.comments %>
Any recommendations?
Upvotes: 1
Views: 608
Reputation: 894
I set up a test application using the kaminari gem for pagination. This is what my my music video controller's show action looks like:
def show
@music_video = MusicVideo.find(params[:id])
@comments = @music_video.comments.page(params[:page]).per(3)
end
And here is what my show view looks like:
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong> <%= @music_video.name %>
</p>
<% @comments.each do |comment| %>
<p>
Comment: <%= comment.text %>
</p>
<% end %>
<%= paginate @comments %>
<%= link_to 'Edit', edit_music_video_path(@music_video) %> |
<%= link_to 'Back', music_videos_path %>
It is working and the pagination is showing up for me.
I think one thing i see directly is that you should use <% @comments.each do |comment| %>
instead of <% @music_video.comments.each do |comment| %>
because the way you have it now it will display all comments for the video regardless of what page you are on. If you had 6 comments and wanted 3 per page you would see the pagination with the two pages because you're running your pagination based off of @comments
and you would end up seeing all 6 comments on both pages because you're doing your .each
with @music_videos.comments.each
.
So, at least using @comments
in both places would be a start. And make sure you're using <%= paginate @comments %>
for the pagination. If you use this in your controller and view what do you get? Do you see any comments?
Also, Ryan Bates has a great screencast on Kaminari as well: http://railscasts.com/episodes/254-pagination-with-kaminari (that site is a great resource for rails questions)
Upvotes: 2