My new controller action:
controller do
layout 'active_admin'
def index
#pages = Page.all
end
end
After refresh the page i received:
undefined method `base' for nil:NilClass
render view_factory.layout
What should I do for fixing this?
I start rewriting controller action because i received this message for my index action:
undefined method `num_pages' for #<Array:0x0000000b860eb0>
render renderer_for(:index)
Maybe anyone know how fixing this?
The initial undefined method 'num_pages' for #<Array:0x0000000b860eb0> may be occurring if you have an instance variable set in a before_filter in ApplicationController with the plural name of a model as I did. The bug is reported here.
Would need to see the code on the view page for this but it sounds to me like you are making a call for num_pages on an object that is an array class. Since Ruby's array class has no num_pages method, it is throwing an error.
Jamie you are right! But then i received this message for my index action:
undefined local variable or method `per' for ActiveRecord::Relation
And I fix this problem by doing this:
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
The bug is reported here.
Related
I have a function like this to set global parameter in a controller of ruby on rails.
def set_author
#author = Author.find(params[:id])
end
and I set it value by calling before_action
before_action :set_author,only: %i[ show edit update destroy ]
After render the form it will get below error unless I set the #author manually in each function. This is really insane as other controllers work well except this controller after I changed it name.
NoMethodError at /authors/1/edit undefined method `to_key' for #Author::ActiveRecord_Relation:0x00005634de007f58 Did you mean? to_set to_ary
I'm trying to add an around_filter to a controller in ActiveAdmin. I get an undefined method error when I try to add the filter. Example:
ActiveAdmin.register Event do
controller do
around_filter :my_filter
def my_filter
yield
end
end
end
When I try it out, I get:
"undefined method `my_filter' for #<Admin::EventsController:0x0000010de3a798>"
My project is using Rails 3, if that's relevant. What am I missing here?
Update: This was due to a very silly syntax error. Rather than something like the above, I had misplaced my method definition, something like this:
ActiveAdmin.register Event do
controller do
around_filter :my_filter
# lots of stuff here...
end
def my_filter
yield
end
end
so I was declaring the around filter, but defining it outside the controller.
Filter method should be inside controller
ActiveAdmin.register Event do
controller do
around_filter :my_filter
# lots of stuff here...
def my_filter
yield
end
end
end
in my project i have controller in namespace admin and I'm using breadcrumbs_on_rails to build breadcrums. My controller looks like:
module Admin
class FaqsController < Admin::ApplicationController
include FaqsHelper
load_and_authorize_resource
add_breadcrumb t('faqs.faqs_list') , :faqs_path #this line makes the problem
def index
#faqs = #faqs
add_breadcrumb t('faqs.faqs_list')
end
def new
add_breadcrumb t('faqs.new')
end
#other code ommitted
end
end
i can use t method in new, edit and other controller action but when this 't' is not in the controller action i have the follwoing error:
undefined method `t' for Admin::FaqsController:Class
Any ideas?
Use I18n.t instead of just t.
I can suggest to extend your class with
extend ActionView::Helpers::TranslationHelper
It will allow you to use #t and #l helpers.
Thanks Skydan but extend would work only for modules. I made it work by adding include ActionView::Helpers::TranslationHelper to my controller.
I'm using declarative authorization in my rails app, and I have the following controller:
class OrganizationsController < ApplicationController
filter_resource_access attribute_check: true
def index
#organizations = Organization.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #organizations }
end
end
end
However when I try to access the view for that action, I get the error
undefined method 'each' for nil:NilClass on the line where I try to access the #organizations instance variable. It works when I comment out the filter_resource_access line.
Any thoughts?
the error is because your instance variable is Nil, is why it says undefined method each for nil:NilClass. filter is a method that runs either before or after the controller, in this case i'm not exactly sure what that method means but its not letting you access the data until after the controller is my guess.
Apparently I forgot to include the declarative_authorization gem in my Gemfile. :shame:
All is working now.
I'm trying to override the index action of the ActiveAdmin controller for it to display results for the current_user instead of all results.
controller do
def index
#user_tasks = UserTask.where(:user_id => current_user.id).page(params[:page])
end
end
When accessing ActiveAdmin, an exception in thrown:
ActionView::Template::Error (undefined method `base' for nil:NilClass):
1: render renderer_for(:index)
I'm using rails 3.1 and the latest ActiveAdmin version. gem "activeadmin", :git => 'https://github.com/gregbell/active_admin.git'.
I don't know why but
controller do
def index
index! do |format|
#user_tasks = UserTask.where(:user_id => current_user.id).page(params[:page])
format.html
end
end
end
did the trick.
This is not required any more.
ActiveAdmin 0.4.4 now supports scoping queries without overriding this method.
please see here: http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries
If your administrators have different access levels, you may sometimes
want to scope what they have access to. Assuming your User model has
the proper has_many relationships, you can simply scope the listings
and finders like so:
ActiveAdmin.register Post do
scope_to :current_user
# or if the association doesn't have the default name.
# scope_to :current_user, :association_method => :blog_posts
end
Let override the action like this:
controller do
def scoped_collection
# some stuffs
super.where("type = ?", "good")
end
# other stuffs
end
By this way, you also can run the export functions (to xml, csv, ...) normally with new collection that you have overridden.
In my test, it just works for where condition and scope, not for limit.
Refer from this: https://github.com/activeadmin/activeadmin/issues/642