Rails: Rendering with Pagination Issues of a Comment Model - ruby-on-rails

The current page that I have been working on is a page where a user can create a comment under a specific micropost. All the microposts are shown on the same page and those microposts are paginated. Under each specific micropost, there are comments and those comments should be paginated and the issue I am currently having is that the pagination only happens when the HTML code is this:
HTML
<div id='comments'>
<%=render #comments %>
<%= will_paginate #comments, :class =>"pagination" %>
</div>
But the thing is when it is like that only the comments for the first micropost will show up on every single micropost rather than the specific micropost comments. So I figured why not put the code like this:
HTML
<div id='comments'>
<%=render micropost.comments %>
<%= will_paginate #comments, :class =>"pagination" %>
</div>
This basically set all the specific comments in the right micropost BUT it didn't paginate but the pagination links showed, so I figured putting this would work but it didn't, got an error:
HTML
<div id='comments'>
<%=render micropost.comments %>
<%= will_paginate micropost.comments, :class =>"pagination" %>
</div>
I am very confused and unsure what to do and yes the micropost is suppose to have no #
I would appreicate any suggestions that would help! Thank you!
My User#Show
User Controller
class UsersController < ApplicationController
def show
#user = User.find_by_id(params[:id])
#school = School.find(params[:id])
#micropost = Micropost.new
#comment = Comment.new
#comment = #micropost.comments.build(params[:comment])
#comments = #micropost.comments.paginate(:page => params[:page], :per_page => 10)
#microposts = #user.microposts.paginate(:per_page => 10, :page => params[:page])
end
end

This will work, but the fact that you need to do an assignment inside your view tells you this is probably not the way to do it:
<div id='comments'>
<% comments = micropost.comments.paginate(:per_page => 10, :page => params[:page]) %>
<%= render comments %>
<%= will_paginate comments, :class =>"pagination" %>
</div>
Do realize that the micropost & comments both use the same page parameter. So that if you want to view the second page of comments of any of the given microposts you will also switch to the second page of microposts. I would reconsider showing all this information on the same page.

Related

Running into undefined method `total_pages' with will_paginate

My goal is to have a tag cloud within my posts page which will allow the user to filter posts by clicking on a tag. However, I am running into an undefined method 'total_pages" error after making the following changes to my Post_Controller method:
class PostsController < ApplicationController
def index
if params[:tag]
#posts = Post.tagged_with(params[:tag])
else
#posts = Post.visible_to(current_user).where("posts.created_at > ?", 7.days.ago).paginate(page: params[:page], per_page: 10)
end
end
end
I am trying to use the acts-as-taggable-on gem, and this logic will show me the posts with the appropriate tags.
The issue happens in the posts/index.html.erb view:
<div class="row">
<div class="col-md-8">
<h1> Trending </h1>
<p class="lead"> Active posts this week </p>
<div id="tag_cloud">
Tag Cloud:
<% tag_cloud Post.tag_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<%= render partial: 'posts/post', collection: #posts %>
<%= will_paginate #posts %>
</div>
<div class="col-md-4">
</div>
</div>
The will_paginate line will not render all the posts on that page. The work around is getting rid of <%= will_paginate #posts %> and replacing
#posts = Post.visible_to(current_user).where("posts.created_at > ?", 7.days.ago).paginate(page: params[:page], per_page: 10)
with #posts = Post.all. However, this gives me an entire page of posts, which is ugly. Does anyone know why I am running into an undefined method 'total_pages' error?
It looks like when you're sending a tag (params[:tag]) it is fetching posts with
#posts = Post.tagged_with(params[:tag])
which is lacking the will paginate call. I believe you could get it working by adding the will paginate scope, like this:
#posts = Post.tagged_with(params[:tag]).paginate(...)

nested attribute pagination error (kaminari)

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?
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)

Render in rails a view

Sorry for a simple question but i am a bit confused trying to follow ruby on rails tutorial book
I am at the chapter 10 and confused, yes i trick a bit my version for learning purpose
So I have a controller called customer
customers_controller.rb
def show
#customer = Customer.find(params[:id])
#posts = #customer.posts
end
I then have the following folder
_post.html.erb
Welcome to a post
Which his called from the show customer file has follow
/view/customer/show.html.erb
<% provide(:title, #customer.name) %>
<aside class="customer_show_nav">
<h1><%= #customer.name %></h1>
<%= #customer.email %>
</aside>
<div class="events">
<%= render #posts %>
</div>
But when loading nothing his appearing not even Welcome to a post. What i am doing wrong?
Thanks in advance. I am following the tutorial http://ruby.railstutorial.org/chapters/user-microposts#top 10.22
if _post.html.erb is in view/customers
you do this in view/customers/show.html.erb
<%= render 'controller_where_post_lives/post' %> which will look for customers/_post.html.erb
Sometimes, you also need = in <%= %> with rails 3
Also, show is used to show one item, and index is used to show all items.
So to render you will do
<%= render :partial => "post", :collection => #posts %>
Edit:
When you call render you give it the view, no objects here. Unless like above passing a collection.
http://guides.rubyonrails.org/layouts_and_rendering.html
Also if you just want to render text you can do render :text => "OK"
Look for partials explanation.

Ruby on rails tags [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding tags to posts in Ruby on Rails
I have a simple yet tricky (for me at least) question... what would be the best way to create tags in my sample application blog?
I am adding tags using act-as-taggable, but can I make them clickable so that when people click on it, all posts with that tag would be shown?
I can't quite get it O___o
Any help is super appreciated!
Here is what i did so far:
in my posts controller
def tagged
#posts = Post.all(:order => 'created_at DESC')
#tags = Post.tag_counts_on(:tags)
#tagged_posts = Post.tagged_with(params[:tags])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #posts }
end
end
then in my posts/show view
<% unless #post.tags.empty? %>
<div class="category">Category:
<% #post.tags.each do |t| %>
<%= link_to t.name, {:tag => t.name, :action => "tagged", :controller => 'posts'} %>
<% end %>
in my posts/tagged view
<% #tagged_posts.each do |post| %>
<div class="entry">
<h2><%= link_to post.title, post %></h2>
<div class="content"><%= sanitize blog_truncate(post.content, :words => 100),:tags => %w(strong, b, a) %><br /><%= link_to "[read more]", post %>
</div>
</div>
<% end %>
I kind of loosely followed this guide:
http://g-p.si/posts/tagging-with-acts-as-taggable-on
my issue is that the tag is clickable on my posts/show page, I get redirected to my tagged page and the url looks like mysite/tagged?tag=ruby
But my tagged page is blank...
Each link for the tag that the user clicks on should have a href of something like:
/posts?tag=my_tag_name
And then in the posts controller
class PostsController
def index
if params[:tag].present?
#posts = Post.where(tag: params[:tag])
else
#posts = Post.all
end
end
end
Note this code is not tested and I've never used acts as taggable so you should first make sure how to query for tagged posts.
It's almost right, thanks for the hint! you have to use
#posts = Post.tagged_with(params[:tag])
instead of
#posts = Post.where(tag: params[:tag])
and it works like magic! :)

How do I display "wall posts" on a user's page?

I'm trying to show "wall posts" that all users have made on a specific user's page, but I'm having difficulty showing the correct posts.
Users controller:
def show
#user = User.find_by_cached_slug(params[:id])
#posts = Post.find_all_by_poster(params[#user.id])
if signed_in?
#post = Post.new
end
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #user }
end
end
Post form:
<%= form_for #post do |f| %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.hidden_field :user_id, :value => current_user.id %>
</div>
<div class="field">
<%= f.hidden_field :poster, :value => #user.id %.
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
When a user creates a post the current_user id is saved in the posts table as the :user_id and the user id of the user's page they're posting on is saved as :poster. This part is working, but I don't understand how to render #posts so that only the posts that have been made on that user's page appear.
The approach I'm trying is somehow showing/filtering? all posts where :poster matches the user id of the user's page; however, I don't know how to make this work or if there is a better way. Any help?
Note: I'm using the slugged gem. In the Users/show view, I use <%= render #posts %>. Eventually, I'd like for users to be able to comment on posts, if that affects any design decisions.
Thanks very much for your help! Please let me know if any more information is needed.
I think this line is the issue:
#posts = Post.find_all_by_poster(params[#user.id])
#user is the the record for the user who wall is now being viewed. (right?) You need all the posts that were posted to that users wall that is all the Post records where poster == #user.id. There is no need to look in the params hash for this. I believe that this line should instead be:
#posts = Post.find_all_by_poster(#user.id)
If this is incorrect, it means that I am not clear how your show action is meant to work. There are two different things that could be #user. By convention this should always refer to the record of the person at the other end of the connection. In your case, it looks like it instead refers to the person whose wall is being viewed. Is that correct?

Resources