Show child/nested attributes in ActiveAdmin index view column - ruby-on-rails

I'm trying to show the value of a nested attribute in ActiveAdmin.
My code is as follows:
index do
column :code
column 'Sales Agent' do |client|
client.sales_agent.agent_name
end
end
This gives me:
NoMethodError in Admin/client_branches#index
Showing /Users/constantlm/.rvm/gems/ruby-1.9.2-p290/gems/activeadmin-0.4.3/app/views/active_admin/resource/index.html.arb where line #1 raised:
undefined method `agent_name' for nil:NilClass
I don't understand why this doesn't work, because when I do the following:
index do
column :code
column 'Sales Agent' do |client|
raise client.sales_agent.agent_name.inspect
end
end
It outputs:
RuntimeError in Admin/client_branches#index
Showing /Users/constantlm/.rvm/gems/ruby-1.9.2-p290/gems/activeadmin-0.4.3/app/views/active_admin/resource/index.html.arb where line #1 raised:
"Peter John"
Which is what I would expect the first statement to do as well (referring to the "Peter John"), but it's not. What am I missing?

Clear your records and start with new records. Additionally try adding
client.sales_agent.agent_name if !client.sales_agent.nil?

Related

undefined method `+' for nil:NilClass - Values are not null

I am trying to create a method that loops through some objects and if a certain attribute is true, adds to the cost of the lesson to the money received, but keep getting the error undefined method `+' for nil:NilClass.
Here is the piece of code (the line producing the error start #activity.update_attribute):
def show
#activity = Activity.find(params[:id])
#participants = Participant.all.where(:activity_id => #activity.id).map(&:user_id).uniq
#all_participants = Participant.all.where(:activity_id => #activity.id)
#all_participants.each do |a_participant|
if a_participant.paid
#activity.update_attribute(:money_received, #activity.money_received + #activity.cost)
end
end
#users = Array.new
#participants.each do |participant|
#users.push(User.find(participant))
end
end
And here is a screenshot from my database to show that neither is a null value:
Here is the error message that I get when running the application:
Any help would be greatly appreciated
Many thanks

Rails 4 Error: NoMethodError - undefined method

I am struggling ordering my loop through my Devise Users by the upvotes on tools created by them.
Inside of that loop I get the value I would like to use to order the elments with:
user.tools.map(&:cached_votes_up).sum
That's my loop:
- #users.order(user.tools.map(&:cached_votes_up).sum).each do |user| #incorrect!
And my controller:
#users = User.all
Without order in my loop everything runs fine, with it this error appears:
NoMethodError in Users#index
Showing c:/Users/Jonas/gitapps/ocubit/app/views/users/index.html.haml where line #21 raised:
undefined method `tools' for #<Class:0xac2db78>
I am grateful for each help!
You can use joins
#users = User.joins(:tools).group("users.id").order("SUM(tools.cached_votes_up)")

Undefined method 'id' for Class, but the console loads fine

I can access the shopping_list_products loaded in this API call from the console with no error.
I get a 500 on the API, however:
ActionView::Template::Error (undefined method `id' for #<Class:0x007f6cca7e1dd0>):
2015-01-26T23:51:07.608697+00:00 app[web.1]: 1: collection :#shopping_list_products
2015-01-26T23:51:07.608699+00:00 app[web.1]: 2: extends 'api/v1/shopping_list_products/_show'
index.json.rabl:
collection :#shopping_list_products
extends 'api/v1/shopping_list_products/_show'
show.json.rabl:
object :#shopping_list_product
extends 'api/v1/shopping_list_products/_show'
_show.json.rabl:
object :shopping_list_product
attribute(:id, :if => lambda { |m| m.id })
attributes :shopping_list_retailer_id, :product_id, ...
... more attributes
child :product, partial: 'api/v1/products/_show'
child :product_category, partial: 'api/v1/product_categories/_show'
node(:url) do |shopping_list_product|
api_v1_schedule_requisition_plan_shopping_list_shopping_list_retailer_shopping_list_product_path(#schedule, #shopping_list_retailer, shopping_list_product, format: :json)
end
EDIT: I removed the id attribute and then ran into the next error, "undefined method shopping_list_retailer_id for Class:". Why is this happening?
EDIT: Found out it's my code called from the controller.. if I return
#shopping_list_retailer.shopping_list_products
it works fine.
But I do this instead:
api :GET, '/schedule/:schedule_id/requisition_plan/shopping_list/shopping_list_retailers/:shopping_list_retailer_id/shopping_list_products', 'List all shopping list products for Shopping List for Requisition Plans for Schedule in the database'
param :query, String, desc: "Scoped_search style query string"
def index
#shopping_list_products = ShoppingListProductsIndexQuery.new(#shopping_list_retailer, params[:query]).shopping_list_products
end
class ShoppingListProductsIndexQuery
attr_reader :shopping_list_retailer, :query
def initialize(shopping_list_retailer, query)
#shopping_list_retailer = shopping_list_retailer
#query = query
end
def shopping_list_products
#shopping_list_retailer.shopping_list_products.ordered_by_product_category_type_and_product_category_name_and_product_name.search_for(query)
end
end
Still confused why undefined method id for Class is hit in the rabl view.
Your error message (undefined method 'id' for #<Class:0x007f6cca7e1dd0>) is saying the undefined method is on an instance of Class instead of an instance of ShoppingListProduct (or whatever your actual class name is).
This means the wrong thing is being rendered.
Probably because you are using:
object :#shopping_list_products
# and
object :#shopping_list_product
Instead of:
object #shopping_list_products
# and
object #shopping_list_product
Remove the :s.
Also, in your _show.json.rabl file, you really don't need
object :shopping_list_product
as this is ignored when the RABL template is being used as a partial (https://github.com/nesquena/rabl/wiki/Reusing-templates)

Ruby on Rails - Active Admin Strange Errors

On Rails 4. I'm getting some strange errors when I try to load index pages in Active Admin. They were all working fine before, but suddenly I started getting this message (for this example I loaded my Categories index but it is happening for most of them):
NoMethodError in Admin::Categories#index
Showing c:/Ruby200/lib/ruby/gems/2.0.0/bundler/gems/active_admin-3fb7f03335b1/app/views/active_admin/resource/index.html.arb where line #1 raised:
undefined method `validators_on' for Ransack::Search:Class
Extracted source (around line #1):
insert_tag renderer_for(:index)
Application Trace | Framework Trace | Full Trace
config/initializers/form_builder.rb:12:in `label'
I did a search for that method name and it returned only this:
In form_builder.rb
if object.class.validators_on(method).map(&:class).include? ActiveRecord::Validations::PresenceValidator
if options.class != Hash
options = {:class => "required"}
else
options[:class] = ((options[:class] || "") + " required").split(" ").uniq.join(" ")
end
end
I can view the dashboard and individual row pages fine, but when I go to edit a record I get this:
TypeError in Admin::Categories#edit
Showing c:/Ruby200/lib/ruby/gems/2.0.0/bundler/gems/active_admin-3fb7f03335b1/app/views/active_admin/resource/edit.html.arb where line #1 raised:
no implicit conversion of String into Array
Extracted source (around line #1):
insert_tag renderer_for(:edit)
Application Trace | Framework Trace | Full Trace
config/initializers/form_builder.rb:16:in `label'
I have no idea what this means...would it be better to re-install Active Admin/regenerate its assets? Is that a safe thing to do? If so, how do I do that? Or, is there a simple fix to these error messages. Thanks for any help.
I encountered the same issue and have a fix for you. The issue is that the very clever initializer that automatically adds a CSS * to required fields is not compatible with the Ransack search that ActiveAdmin uses. The solution is to check that the model responds_to the necessary method before invoking it:
class ActionView::Helpers::FormBuilder
# http://blog.pothoven.net/2012/10/self-marking-required-fields-in-rails.html
alias_method :orig_label, :label
# add a 'required' CSS class to the field label if the field is required
def label(method, content_or_options = nil, options = nil, &block)
if content_or_options && content_or_options.class == Hash
options = content_or_options
else
content = content_or_options
end
options = add_required_class(options) if presence_required?(method)
orig_label(method, content, options || {}, &block)
end
private
def add_required_class(options)
return { class: 'required' } unless options.class == Hash
new_class = ((options[:class].to_s || '') + ' required')
.split(' ').uniq.join(' ')
options.merge!(class: new_class)
end
def presence_required?(method)
object.class.respond_to?(:validators_on) &&
object.class.validators_on(method).collect(&:class)
.include?(ActiveRecord::Validations::PresenceValidator)
end
end
AA works with Rails 4 and Ruby 2.1.1, however, you have to take AA from the master branch at Github. Please note that AA switched from the "meta_search" gem to "ransack" which is not API-compatible - so some things are sure to break.
Maybe you use custom filters? I had your kind of errors due to custom filters based on scopes since "ransack" does not feature anything like search_method from "meta_search". Here's how I work around this, just in case:
https://github.com/activerecord-hackery/ransack/issues/345
https://github.com/activerecord-hackery/ransack/issues/288

ActiveRecord::AssociationTypeMismatch when setting an association

This is the error:
ActiveRecord::AssociationTypeMismatch in VideosController#update
Topic(#2173382840) expected, got String(#2148246520)
It's from this method:
def assign_topics
if #topic_names
self.topics = #topic_names.each do |name|
Topic.find_or_create_by_name(name)
end
end
end
What is this error saying? I don't understand there being a type "topic" when topic is a model for me...
"each" returns the original array or enumerable, try using "map" instead

Resources