Rails_Admin lookup in list - ruby-on-rails

I have this model "Schedule" with attributes :id, :augurid, #and so on.
And I have another model "Augur" with attributes :id, :name, :email #and so on.
Now I want to display augur.name in the Schedule page in Rails_Admin, and my code is like following:
config.model "Schedule" do
list do
fields :id, :weekdays, :occupied, :starting, :duration
field :augur do
pretty_value do
augur = Augur.find(:augurid)
augur.name
end
end
end
edit do
fields :id, :augurid, :weekdays, :occupied, :starting, :duration
end
show do
end
end
But this code is not working, I don't know why since I'm new to rails, and I've checked the documentation of rails_admin, and looks like there's no solution to this problem.
Can anyone help me?
And I think the problem is around
augur = Augur.find(:augurid),
because if I change the :augurid to 5, it will display the right augur name. But how do I do that for every record in schedule?

You should just be able to do add object_label_method :name to the rails_admin configuration for the Augur model:
config.model "Augur" do
object_label_method :name
...
end
Is that what you're looking for?

As #JKen13579 mentioned, :object_label_method :name can do it for you. But if you really want to access the "current object" in your rails_admin callbacks, there is a way:
list do
field :augur do
pretty_value do
bindings[:object].name
end
end
end
This will show the name of the augur and only affect the list view.

Related

ActiveAdmin Globalize create index filters

I'm using Globalize and ActiveAdmin, and I've now installed a gem from a fork of ActiveAdminGlobalize
Everything that is described in the readme is working, but I'd like to add a filter to the Active Admin Index.
So, for the model stuff.rb
class Stuff < ApplicationRecord
translates :name
active_admin_translates :name do
validates_presence_of :name
end
end
And the class in app/admin/stuff.rb
ActiveAdmin.register Stuff do
index do
translation_status
column :name
end
filter :name
end
How do I make the filter :name to work?
Thanks
I'm using the regular ActiveAdmin gem and, after scratching my head for quite some time, found that the following works:
filter :translations_name_contains, as: :string
Of course you can change name with any other attributes you have translated with Globalize
filter :translations_title_contains, as: :string
To tie everything up nicely, I like to customize the label to avoid the default one AA creates:
filter :translations_title_contains, as: :string, label: "Search", placeholder: "Search page title..."
Hope this helps, thanks!

Fill hstore attribute in Active Admin

I have an hstore attribute named salary to store min and max values. To fill this in active admin (+ simple_form gem), I wrote the following:
ActiveAdmin.register Job do
permit_params :salary
form do |f|
f.simple_fields_for :salary do |salary|
salary.input :min, label: 'Min Salary'
salary.input :max, label: 'Max Salary'
end
f.actions
end
end
This shows me the right form with job[salary][min] and job[salary][max] inputs but doesn't save them when updated/created.
What am I doing wrong? Thanks.
hstore requires some special ActiveAdmin code to work on edit and creation. You will need to write a custom controller action. Something like:
ActiveAdmin.register Job do
controller do
def update
unless params[:job][:min].empty?
job = Job.find(params[:id])
job.min = params[:job][:min]
job.save
end
unless params[:job][:max].empty?
job = Job.find(params[:id])
job.max = params[:job][:max]
job.save
end
# Call the default update action
super
end
end
end
The above snippet is assuming that you are using something like
store_accessor :salary, :min, :max
to access your hstore properties on your Job model.

rails_admin field configuration

I'm having trouble configuring the fields of my models.
I can change the name of the label as directed here: https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
But when I do that I lose the habtm fields.
Here is the code I have for my Post Model BEFORE changing the field label
config.model Post do
weight 1
label_plural "Aktuelles"
label "Aktuelle"
include_all_fields
exclude_fields :created_at, :tag_list, :post_type, :post_tag_list, :slug
field :body do
ckeditor true
end
end
and here is what the habtm Page model relationship looks like when editing a Post.
Now I want to change the "Pages" field label to "Seiten".. and I try like this
config.model Post do
weight 1
label_plural "Aktuelles"
label "Aktuelle"
include_all_fields
exclude_fields :created_at, :tag_list, :post_type, :post_tag_list, :slug
field :page_ids do
label "Seiten"
end
field :body do
ckeditor true
end
end
and get this in the UI
How can I change the "Pages" label to "Seiten" without this side effect?
Thanks in advance for your help and time!
instead of page_ids
# remove below line
field :page_ids do
label "Seiten"
end
# Add this
field :pages do
label "Seiten"
end

Rails 3 and ActiveAdmin. Filter is displaying Objects not the company name

I have a list of customers but on the filter section on the right column, I get a list like this #<Customer0X0B500> in the select menu. How can I display the company_name attribute of Customer instead?
Figured it out, thanks!
filter :customer, :collection => proc {(Customer.all).map{|c| [c.company_name, c.id]}}
i'm not sure I understand you but probably you should define to_s method inside your Customer class e.g.
class Customer
def to_s
self.company_name
end
end
it would be easier if you shared some code
class Customer
def display_name
self.company_name
end
end
Defining display_name instead of to_s works better...
to_s may be called automatically by other objects.
display_name only affects ActiveAdmin
You can also define:
show :title => :display_name
This will make your company_name appear as the title on the view pages instead of Company #x.
To make ActiveAdmin display select menu properly try in Model.rb:-
alias_attribute :name, :category_name

How do I get Index-tank to do conditional indexing?

After a long conversation with the fine people at IndexTank, I was not really sure on how to fix my problem, and I was wondering if anyone could help me.
I have an article model that belongs to a user model. This article model also has a boolean attribute called anonymous, which, if set to true gives the user the option to post the article without his name being shown.
Article
belongs_to :user
attr_accesible :anonymous, :user_id
User
has_many :articles
My problem is that if the article is posted as anonymous. I don't want tanker to search within the author name field, but I want it searching every other field. I tried to do this with an if else statement where I would normally put the "tankit" block, but that does not work.
Is there a way I could put the tankit block into a model method and use a validation call back like this?
def anon_index
if self.anonymous
tankit 'my_index' do
indexes VARIABLES ETC BUT NOT the user_ attributes
end
else # if anonymous is false
tankit 'my_index' do
indexes :title
indexes :body
indexes :user_penname
indexes :user_firstname
indexes :user_lastname
end
end
end
I was thinking either this or putting an if else statement where the "tankit" block declaration goes, but neither of those seem to, unless I'm doing something wrong.
how does this look?:
class Article < ActiveRecord::Base
tankit 'my_index' do
indexes :title
indexes :body
indexes :custom_penname
indexes :custom_firstname
indexes :custom_lastname
end
def custom_penname
if self.anonymous
'anonymous'
else
self.user_penname
end
end
def custom_firstname
#same for first name
end
def custom_lastname
#same for last name
end
end
Same approach, different scenario:
https://github.com/adrnai/rails-3-tanker-demo/blob/master/app/models/comment.rb

Resources