I just installed will_paginate and it seems to be working fine except is not displaying the links at the bottom of the page. Here is my controller:
def index
#projects = Project.paginate(:page => params[:page], :per_page => 9)
#user = User.new
respond_to do |format|
format.html
format.json { render json: #projects }
end
end
and this is in my index.html.erb:
<% will_paginate #projects %>
<% #projects.each do |project| %>
<div class ="col-sm-3 project">
<div id="project-index-pic">
<%= link_to image_tag(project.url), project_path(project), class: "project-index-pic" %>
</div>
// .....more stuff ....
<%end%>
As I said, it seems to be working as far as displaying the proper amount of results (9) on the first page, and if I change that to 2 or 3 it responds accordingly. I am just not getting the links at the bottom of the page to take me to the next 9 results. In other threads people seem to see html elements associated with the will_paginate but I don't. I ran the troubleshooting lines in console:
[1] giving_tree(main)> defined? WillPaginate
=> "constant"
and:
[2] giving_tree(main)> ActiveRecord::Base.respond_to? :paginate
=> true
So that looks good. Checked the docs and a couple threads and I just don't see what I am missing. Thanks!
It's the little things.
<% will_paginate #projects %>
should have been:
<%= will_paginate #projects %>
Related
I'm discovering the gem will_paginate which is great ! But I'm facing a problem of using. I'm building a group>post>comments app, so in my group show page i'm displaying posts and their comments. To limit the numbers of queries, i'm using includes method like this :
Group_controller :
def show
#posts = #group.posts.order(upd_at: :desc).includes(:user).includes(comments: :user).paginate(page: params[:page], per_page: 10)
end
So I would like to also paginate my comments. Do you know a way to do that ?
My code :
Group_show =
<h1>Groupe <%= #group.name %></h1>
<div class="post_list<%=#group.id%>">
<%= render #posts %>
</div>
<%= will_paginate #posts, renderer: BootstrapPagination::Rails %>
And my posts/_post =
<% #comments = post.comments %>
<ul id="comment_list<%=post.id%>">
<%- if #comments.any? %>
<%= render #comments, post: post %>
<%= will_paginate #comments, renderer: BootstrapPagination::Rails %>
<% end %>
</ul>
By the way if you have a method to define #comments directly in the Groups_controller(show), it can be really useful ;)
Not 100% tested, but I think this should work. Do you know how all these components work? If not, let me know and I can explain.
posts/_post
<% #comments = post.comments.order(created_at: :desc).limit(3) %>
<ul id="comment_list<%=post.id%>">
<%- if #comments.any? %>
<%= render #comments, post: post %>
<%- if post.comments.offset(3).exists? # this is more efficient than count > 3 bc it quits counting after 3 %>
<!-- the below link_to creates: href="/posts/:id/comments" ... -->
<!-- ... and `remote: true` makes that an ajax request -->
<li><%= link_to "more", comments_post_path(post), class: "more-comments-btn", remote: true %></li>
<% end %>
<% end %>
</ul>
config/routes.rb
resources :posts do
# `member do` is explained here: http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
member do
get :comments
end
end
posts_controller.rb
# GET /posts/:id/comments
def comments
#post = Post.find(params[:id])
#comments = #post.comments.order(created_at: :desc)
# since you requested this url via ajax with `remote: true` rails will automatically render `posts/comments.js.erb` ...
# ... rather than a typical html request where rails would automatically render `posts/comments.html.erb`
end
views/posts/comments.js.erb
// some people like to use render #comments as shorthand like you did above. I'm a fan of being more explicit like the below
$("#comment_list<%= #post.id %>").html("<%= escape_javascript(render partial: 'comments/comments', locals: {comments: #comments, post: #post}) %>");
// now remove the more comments button
$("#comment_list<%= #post.id %>").find(".more-comments-btn").remove();
The documentation here explains the use of remote: true for ajax requests. Scroll down to section "3.1.2 link_to" and then section 5.1 for the controller and js.erb view.
EDIT:
Basically it is because I am making the pagination links appear in a bootstrap class - col-xs-5 so everything appeared to the right of the page. When I commented it all out, the links worked fine but everything is in the center of the page now like normal non-Bootstrap content..what to do?
EDIT:
Is this a browser thing? I'm on a Macbook Pro OSX Yosemite, using the Safari browser.
Basically, if I select page 2, that URL is correctly shown in the browser URL field: localhost:3000/users/showtasksforuser?page=2, but the entries for page 2 are not rendered in place of page 1 unless I reload the page2 URL manually by pressing enter.
(As a sidenote, users/showtasksforuser works and users/showtasksforuser/?page=1 is not needed for the first page).
In users/showtasksforuser.html.erb:
<% #tasks.each do |task| %>
</br></br>
<div style="text-align:center"><%= task.name %></div>
<% end %>
<%= paginate #tasks, :remote => true %>
In my Users controller:
def showtasksforuser
#tasks = Task.page(params[:page]).per(1)
end
In my Task model:
paginates_per 1
is the first line after the model declaration.
The problem is that you have not created a showtasksforuser.js.erb file.
<div id="taskList">
<%= render "task_list"%>
</div>
<%= paginate #tasks, :remote => true %>
_task_list.html.erb
<% #tasks.each do |task| %>
</br></br>
<div style="text-align:center"><%= task.name %></div>
<% end %>
users_controller.rb
def showtasksforuser
#tasks = Task.page(params[:page]).per(1)
respond_to do |format|
format.js
format.html
end
end
showtasksforuser.js.erb
$("#taskList").html('<%= escape_javascript(render "task_list")%>')
I recommend changing the name of the action and the files to show_tasks_for_user
I have a similar problem. In my case, this works perfectly well:
<%= paginate #tasks %>
I just remove the :remote => true from paginate, and this makes all the default buttons from Kaminari works.
trying to paginate a list of articles (ideas in my case) which are on my tag show page. So I am listing all ideas that are tagged with "loremipsum". The problem is :per_page => 3 doesn't seem to take effect. All ideas show up (I have 4 for tag "loremipsum") withour error. Pagination links also show at page bottom (but page2 doesn't work).
tags_controller:
def show
#tag = Tag.find_by_name(params[:id])
#ideas = #tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
end
In show.html.erb
<% #tag.ideas.each do |idea| %>....<% end %>
<%= will_paginate #ideas %>
On my idea list page pagination works just fine so no clue.
Any help much appreciated. Thanks in advance!
I would suggest using #ideas.each in your view rather than #tag.ideas.each.
<% #ideas.each do |idea| %> ... <% end %>
<%= will_paginate #ideas %>
That is, I believe the problem you encountered was because you were not iterating over the same collection that you applied the pagination to. Though #ideas will paginate, #tag.ideas will not.
Replace this line
#ideas = #tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
with
#ideas = #tag.ideas.paginate(:per_page => 3, :page => params[:page])
I believe you don't need that all.
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.
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! :)