My Active admin version is 0.3.0, other than that m also using 'Will_paginate' i had made the configuration settings of conflict between kaminari and will_paginate but still m getting this error. i dont know where m making a mistake everything is working fine for other model but not for this model, need help, i have searched also found some links but not get satisfactory answer, it give me error on bold line
ActiveAdmin.register User do
menu :parent => 'Reports'
filter :id
filter :user_id
filter :updated_at
# and other filters
scope :all, :default => true do |user|
User.all
end
scope :active do |user|
User.where("user.status = ?", 'actvie')
end
scope :rejected do |user|
User.where("user.status = ?", 'non_active')
end
actions :index, :show
index do
column "ID" do |u|
link_to u.id, cs_user_path(u)
end
column "Status" do |u|
status_tag(u.status)
end
column "User" do |u|
link_to(u.user.full_name, cs_user_path(u.user)) rescue nil
end
end
collection_action :index, :method => :get do
scope = User.includes([:group,:address]).scoped
scope = scope.order params[:order].gsub(/_/,' ') if params[:order]
#collection = scope.paginate(:per_page => 25,:page => params[:page]) if params[:q].blank?
#search = scope.metasearch(clean_search_params(params[:q]))
**super do |format|**
format.html {
render "active_admin/resource/index"
}
end
end
end
The GitHub documentation of will_paginate states that arrays are not supported that well.
I will suggest using the kaminari gem which includes a helper method for array pagination.
You should be able to take it from here.
Related
I have the following index method
def index
#user = User.find_by_username(params[:user_id])
#search = #user.collections.where(status: params[:status]).search(params[:q])
#search.sorts = 'created_at desc' if #search.sorts.empty?
#collections = #search.result.paginate(page: params[:page])
end
I can then use the following links to display different indexes with that one action
<%= link_to "Collection", user_collections_path(current_user, :status => "Got") %>
<%= link_to "Wantlist", user_collections_path(current_user, :status => "Want") %>
But I want to also be able to link things like this, using other fields to filter the index
<%= link_to "Assembled", user_collections_path(current_user, :progress => "Assembled") %>
and I can't see how to write that. Should I be using scopes or should I have alternative methods in my controller? Is my initial "where" filter a bad way of doing this in the first place?
The best way I found to do this is with the has_scope gem.
Installed that. Named scopes in my model as follows
scope :assembled, -> { where(progress: 'Assembled') }
scope :got, -> { where(status: 'Got') }
scope :want, -> { where(status: 'Want') }
Added the has_scope fields to the controller
has_scope :got, :type => :boolean
has_scope :want, :type => :boolean
has_scope :assembled, :type => :boolean
Then my index method as follows
def index
#user = User.find_by_username(params[:user_id])
#search = apply_scopes(#user.collections).search(params[:q])
#search.sorts = 'created_at desc' if #search.sorts.empty?
#collections = #search.result.paginate(page: params[:page])
end
Then I can link to the index with any combination of those scopes like this
<%= link_to collection, user_collections_path(#user, got: true) %>
<%= link_to assembled, user_collections_path(#user, got: true, assembled: true) %>
No routes and no extra methods. Very clean and expandable.
Use a Gem instead
This is a solved problem. I would use a gem like ransack to do this rather than re-inventing the wheel. It has great helper methods to provide any kind of filtering that you would need.
Routes
Further to San's answer, you'll need to create routes to handle any data you wish to send to your index (for filtering):
You'd do something like this:
#config/routes.rb
resources :users, except: :show do
collection do
get "(:query)", action: :index #-> domain.com/users/:query
end
end
This will allow you to create links like this:
<%= link_to "Link", users_path(current_user, query: "whatever") %>
--
Controller
Inside your controller, all the links will hit your index action, so you'll just need some conditions to make sure you can handle a "non search" request:
def index
#user = User.find_by_username(params[:user_id])
if params[:status].present? && params[:q].present?
#search = #user.collections.where(status: params[:status]).search(params[:q])
#search.sorts = 'created_at desc' if #search.sorts.empty?
#collections = #search.result.paginate(page: params[:page])
end
end
I have some 10,000+ records in my model. In active_admin index page for that model I have set config.paginate = false. So all the 10,000+ records are shown by default.
How can I limit the number to say last 500 records. I have tried using the below method described here, but its not doing anything to the index page.
ActiveAdmin.register Post do
controller do
def scoped_collection
Post.all.limit(500)
end
end
end
set custom # of rows on page with controller before_filter
controller do
before_filter :set_per_page_var, :only => [:index]
def set_per_page_var
session[:per_page]=params[:per_page]||30
#per_page = session[:per_page]
end
end
and render sidebar with corresponding text input (you can render it as a drop-list)
#...
sidebar('Rows on page', :only => :index) do
form do |f|
f.text_field nil, 'per_page', :value => session[:per_page]
end
end
The issue is this code in Active Admin:
module ActiveAdmin
class ResourceController < BaseController
module DataAccess
def per_page
return max_csv_records if request.format == 'text/csv'
return max_per_page if active_admin_config.paginate == false
#per_page || active_admin_config.per_page
end
def max_csv_records
10_000
end
def max_per_page
10_000
end
end
end
end
When the paginate config option is set to false, it defaults to the number value returned by max_per_page. If you're fine with overriding it globally, you can put this in an initializer:
# config/initializers/active_admin_data_access.rb
module ActiveAdmin
class ResourceController < BaseController
module DataAccess
def max_per_page
500 # was 10,000
end
end
end
end
I was looking for an answer to this same question. I was unable to limit the number of records, so instead I have opted for putting a default value in one of my filters that guarantees an empty page when it loads.
(NOTE: I stole this idea from this stackoverflow question here:: Set ActiveAdmin filter default value )
Example::
In this example, I set a filter called "my_filter_id" equal to "0" in the "before_filter" method if all of the parameters are blank.
ActiveAdmin.register MyModel do
before_filter my_filter_id: :index do
params[:q] = {my_filter_id_eq: 0} if params[:commit].blank?
end
end
Use
Post.limit(500) instead of Post.all.limit(500) so it will minimize the latency.
controller do
def scoped_collection
Post.limit(500)
end
end
index :pagination_total => false do
selectable_column
column :id
column :user_name
column :country
column :city
end
Hope this will help someone.
Try below code. Replace something with your model name.
result = Something.find(:all, :order => "id desc", :limit => 5)
while !result.empty?
puts result.pop
end
In Active Admin (RoR), I'm getting data from a table, no problem with that.
But I want to add a sidebar with information thats related with an inner join.
But I don't know how to do this.
This my code:
ActiveAdmin.register Project do
show do |project|
... works ...
end
end
sidebar "Resources for this project", :only => :show do
table_for project.project_resources.where('project_id = ?', project.id) do |row|
column "Resource", Resource.where('id = ?', :resource_id) do |resource|
:resource_name
end
end
end
As you can see, I'm trying to get the resource_name from resources-table. There is a 3th table, project_resource and this contains all the resource_ids for a project_id.
Thanks
Ok, this is the answer I was looking for:
sidebar "Resources for this project", :only => :show do
table_for project.resources do |resource|
column :resource_name do |row|
link_to row.resource_name, admin_resource_path(row)
end
end
end
I am trying to create an alphabetical pagination for rails. I have found some plugins and gems but they come up with errors so I am trying to create a custom pagination which isn't working quite right...WHy is this?
model(userrrrrr.rb)
def self.find_by_group
('A'..'Z').map do |letter|
#users = User.find_by_sql(:all, :conditions=>['LOWER(name) LIKE?', "#{letter.downcase}%"],:order=>'name ASC')
end
#users
end
User helper
def alphabetically_paginate(collection)
available_letters = User.find_by_group
content_tag(:ul, safe(alphabetical_links_to(available_letters)),
:class => "pagination")
end
def safe(content)
if content.respond_to?(:html_safe)
return content.html_safe
end
content
end
def alphabetical_links_to(available_letters)
available_letters = ('A'..'Z').to_a
('A'..'Z').map do |letter|
content_tag(:li, paginated_letter(available_letters, letter))
end.join(" ")
end
def paginated_letter(available_letters, letter)
if available_letters.include?(letter)
link_to(letter, "#{request.path}?letter=#{letter}")
else
letter
end
end
end
views
-('A'..'Z').each do |char|
-if char == user.name[0]
= link_to :action => 'char'
=alphabetically_paginate(#users)
the views is incomplete as I am not sure how to proceed at the moment... I am thinkking about refreshing the page and display the list with only the A or B etc like a seaarch page
This is a bit late to the party, but it might help other users. I've created a gem that allows for alphabetical pagination using SQL queries here: https://github.com/lingz/alphabetical_paginate
It's as simple as:
Controller
def index
#users, #alphaParams = User.all.alpha_paginate(params[:letter], {db_mode: true, db_field: "name"})
end
View
<%= alphabetical_paginate #alphaParams %>
Hope it helps.
Using RoR 2.3.8. I have the following codes:
user.rb
def to_param
"#{login.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
end
people_controller.rb
def show
#person = User.find(params[:id])
if current_user == #person
#posts = #person.posts.paginate(:page => params[:page], :order => order)
else
#posts = #person.posts.by_status('published').paginate(:page => params[:page], :order => order)
end
end
I have a column login in Users database where unique username is. People is just a controller to show some posts created by the user.
I will usually link to the index.html.erb under my people controller with the url http://localhost:3000/people/2 with the following code example in User's posts:
<%=h #post.user_name %>
I want the URL to be http://localhost:3000/people/victor where victor is the login for a user. This url should also actually show the profile show.html.erb in people controller.
What else do I need to do? Thanks!
I use the friendly_ID gem for this sort of thing - it's very straightforward - good luck
I would modify routes.rb, something like this:
match 'people/:login' => 'people#show', :as => 'login'
And then modify a people_controller.rb:
def show
#person = User.where(:login => params[:login]).first
end
edited after additional information
corrected error