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
Related
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
If I am going about this wrong please let me know I can change it. I have a file in config/initializers/payload_signer.rb. I am trying to use this file in the controller that is called device_enrollment_controller.rb.
PayloadSigner.sign(get_profile)
get_profile is a method in the controller that gets the file I need and returns it. PayloadSigner references the other file. When I try to run this (keeping in mind im sure changes will have to be made in payload_signer for it work right) the error I get is uninitialized constant DeviceEnrollmentController::PayloadSigner. This leads me to believe I am referencing the payload_signer.rb file incorrectly. I have tried things like include and load but so far they are not working.
Any help or guidance is appreciated.
Rails Initializers are called before Controllers or Models. So it won't work. Initializers are not intended for this kind of use. Instead I suggest placing your code in a controller before_filter. Either in the ApplicationController or only in those controllers that require it (e.g. DeviceEnrollmentController). Something like this:
class DeviceEnrollmentController # Or ApplicationController
before_filter :sign_payload
protected
def get_profile
# Magic
end
def sign_payload
PayloadSigner.sign(get_profile)
end
end
EDIT: Another example:
class DeviceEnrollmentController
# The filter is only applied to the sign action
# (that's what the :only parameter does).
before_filter :sign_payload, :only => [:sign]
# Browsing to /show, you render this magic button of yours.
def show
# Render page that holds the button
end
# The magic button is bound to the /sign route.
# Clicking on the button calls this action.
def sign
# When you get here, the #sign_payload method
# has already been called.
end
protected
def get_profile
# Magic
end
def sign_payload
PayloadSigner.sign(get_profile)
end
end
class ProductsController < ApplicationController
layout :products_layout
def show
#product = Product.find(params[:id])
end
private
def products_layout
#current_user.special? ? "special" : "products"
end
end
Here when is method products_layout getting executed? There's nowhere I can see that calls the method products_layout so how could the symbol :products_layout be defined?
Rails has implicit rendering, so at the end of the method "show", since you haven't told Rails to do anything different, it will render the app/views/products/show.html.erb template.
In addition, it will look to see what layout you specified. here, you've given a symbol to layout, which Rails takes to mean "execute this method name to find out what layout I should use"
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
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.