I tried the following code but it doesn't work
class BlogsController < ApplicationController
def index
##entry_pages = Paginator.new(self, Entry.count, 10, params[:page])
#entries = Entry.find(:all,
#:limit => #entry_pages.items_per_page,
#:offset => #entry_pages.current.offset,
:order => 'entries.created_at DESC',
:include => :user)
end
end
This is the blog view
<h1>Recently updated blogs</h1>
<% #entries.each do |entry| %>
<p>
<%= link_to entry.user.username, entries_url(:user_id => entry.user) %><br />
'<%= entry.title %>' was posted <%= time_ago_in_words(entry.created_at) %> ago
</p>
<% end %>
I want the items to be paginated like this:
<< [1][2][3] >>
Give the will_paginate GEM a try.
It provides all the features you need to paginate your blog entries.
I can recommend the paginating_find plugin. Here's a tutorial:
http://www.igvita.com/2006/09/10/faster-pagination-in-rails/
Looks like it's now hosted on github.com:
http://github.com/alexkwolfe/paginating_find/tree/master
Related
I'm working on a project on Ruby on rails and wanted to added a search feature. I installed the solr gem following the steps from this site:
http://es.asciicasts.com/episodes/278-busquedas-con-sunspot
right now, this is how my controller looks like:
class DealsController < ApplicationController
def index
# #deals = Deal.paginate(page: params[:page])
#search = Deal.search do
fulltext params[:search]
end
#deals = #search.results
end
private
def deal_params
params.require(:deal).permit(:title)
end
end
this is my model:
class Deal < ActiveRecord::Base
searchable do
text :title, :info1, :page
end
end
and this is my view:
<div class='container'>
<div class='row upper_container'>
<div class='search_container'>
<%= form_tag deals_path, :method => :get, :class => 'navbar-form navbar-left' do %>
<div class='form-group'>
<%= text_field_tag :search, params[:search], class: 'form-control' %>
</div>
<%= submit_tag 'Search', :name => nil %>
<% end %>
</div>
</div>
<% #deals.each_with_index do |d, i| %>
<% if i % 3 == 0 %>
<div class='row middle_container'>
<% end %>
<div class='col-md-4'>
<div class='deal_container'>
<%= d.title %>
<img src='<%= d.photo %>'>
</div>
</div>
<% if (i % 3 == 2) || (i == (#deals.length - 1)) %>
</div>
<% end %>
<% end %>
<div class='text-center'>
<%= will_paginate #deals %>
</div>
</div>
But i get the following error:
undefined method `results' for Ransack::Search>:Ransack::Search
on this line: #deals = #search.results
Any help? thanks!
Jude, change your
#deals = #search.results
to
#deals = #search.result
Are you by chance using the sunspot gem?
If you are using Active Admin and the Sunspot gem for Rails, beware:
they conflict on the search method leading to some very confusing
results.
Active Admin has a dependency on meta_search which provides a
.search() method on Active Record classes. Sunspot attempts to provide
the same method, aliased from solr_search, but only if the method does
not already exist.
In short, searching can be done using solr_search() rather than
search():
#search = Profile.solr_search do
keywords params[:q]
paginate page: params[:page], per_page: page_size
end
#results = #search.results
http://mrdanadams.com/2012/beware-using-active_admin-and-sunspot-rails-gems-together/
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! :)
Here is the scenario:
I have a table which renders a list of groups (id,title,description,etc) like below which is paginated, it also has a search form:
<div id="navigation" style="background:white;border:1px outset cadetblue;">
<p>
<% form_tag groups_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</p>
</div>
<table>
.....
</table>
<%= paginate #groups %>
In my groups_controller.rb I have:
#groups = Group.search(params[:search])
#groups = Group.order("id").page(params[:page]).per(15)
which doesn't seem to be working. The pagination works but when I click
the search button it doesn't do anything.
The search function is implemented in the group.rb model:
def self.search(search)
if search
find(:all, :conditions => ['description LIKE ?', "%#{search}%"])
else
find(:all)
end
end
How do I solve this, how do I make my search form work and return results in a paginated way!
Here is the ultimate answer to this post. All the original poster needed to do was this:
#groups = Group.where("description LIKE '%#{params[:search]}%'").order('id').page(params[:page]).per(15)
In an offline discussion, it wasn't working. turns out he had BOTH will_paginate Gem and kaminari Gem for his pagination. He was crapping out when it came to .per(15). Deleting the will_paginate gem fixed it!
will_paginate isn't being actively upgraded, so if you have will_paginate and you are on Rails 3.X, get rid of will_paginate and use kaminari instead, it's a fairly painless swap out, just a bit of css work to format the pagination.
You need to combine those two calls into one, right now you are overriding the first setting of #groups with the second.
I'd add a scope to your group model
class Group
scope :search, lambda { |val| , find(:all, :conditions=>['description LIKE ?', "%#{val}%"]) }
end
Then you can do
#groups = Group.search(params[:search]).order("id").page(params[:page]).per(15)
In my rails application I wanted to use pagination for names. I'm fetching the names from a postgresql table.
def index
#users = User.order("name").paginate(:page=>params[:page],:per_page=>50)
end
Is there a way of alphabetic pagination that has page numbers as a,b,c..........z
Thanks for your help,
Ramya.
Im doing this too in one of my projects. I didnt use a gem for this. Its really easy using Ranges... For example:
<% ('A'..'Z').each do |char| %>
<% if char==params[:char] %>
<%= link_to :action => 'your_action', :char => char, :class => 'selected_char' %>
<% else %>
<%= link_to :action => 'your_action', :char => char %>
<% end %>
<% end %>
Then in the controller Action you select the Objects by the params[:char] given. For example:
#instance_var = ModelName.where("field LIKE ?", "#{params[:char]}%")
this did it for me. You can easy build a partial out of this and use it for many different Models.
I installed the Acts as taggable on plugin to my 'Post' model, however I'm unable to call up a list of posts tagged with certain tag.
Here's what I have in show.
<%= link_to tag.name, posts_path(:view =>'tag', :tag => tag.name) %><% end %>
Except when I click on it, it shows all posts. I want it to show just the posts tagged with that keyword...what am I doing wrong here?
Thanks for your help
Modify your controller code so that it reads something like:
#posts = Post.tagged_with(params[:tag], :on => 'tags')
Here is what I ended up doing...
index view
<% job.tags.each do |tag| %>
<div class="tag"><%= link_to(tag, jobs_path(tag: tag.name), method: :get) %></div>
<% end %>
index action in the controller
if params[:tag]
#search_term = params[:tag]
#jobs = Job.tagged_with(#search_term)
end