I have models User and Article.
User has the following associations:
class User < ActiveRecord::Base
has_many :articles
and Article's:
class Article < ActiveRecord::Base
belongs_to :user, required: true
I use rails_admin as a control panel and customize a required fields in rails_admin.rb as follows:
config.model Article do
list do
field :id
field :title
field :user_id
field :created_at
end
end
But how to implement that instead of :user_id displays the name of the user? I have a username field in my database.
Thanks!
In your Article model, you can add a custom method that will return the user_name:
def user_name
self.user.username
end
Then, you can use this user_name field in your admin like other model attributes:
config.model Article do
list do
field :id
field :title
field :created_at
field :user_name
end
end
Here's another stab at it...
Rails_admin looks for a "name" or "title" field on the model, if there are none, it will display a somewhat ugly "User#1".
All you need to do in your User Model is to add the following method:
def title
try(:username)
end
I like to go back to this presentation for Rails Admin's best practices: http://www.slideshare.net/benoitbenezech/rails-admin-overbest-practices
It's a bit dated but everything you need is there (around page 14 for your case)
Related
I'm trying to make a form to create new record for a model user which has one billing_information. Billing_information has an attribute account_name that I want to include in the form. I tried using the delegate method but it's not working. It produces :-
error: unknown attribute 'billing_information_account_name' for User.
class User < ActiveRecord::Base
accepts_nested_attributes_for :billing_information
has_one :billing_information, inverse_of: :user
delegate :account_name, to: :billing_information, allow_nil: true
rails_admin do
create do
field :name
field :email
field :billing_information_account_name do
def value
bindings[:object].account_name
end
end
end
end
end
Does anyone has a better solution? Thank you.
Sadly, you won't get help from rails admin in this case, but it can be done.
You have to add a new virtual field and handle in a setter the input. Take a look at this example.
class User < ApplicationRecord
has_one :billing_information, inverse_of: :user
# A getter used to populate the field value on rails admin
def billing_information_account_name
billing_information.account_name
end
# A setter that will be called with whatever the user wrote in your field
def billing_information_account_name=(name)
billing_information.update(account_name: name)
end
rails_admin do
configure :billing_information_account_name, :text do
virtual?
end
edit do
field :billing_information_account_name
end
end
end
You can always create the full billing_information using the nested attributes strategy, meaning add the billing_information field and you'll get a nice form to fill all the information.
I'd like to be able to use the "Add Filter" dropdown so admins can search for posts that have been flagged a particular name.
My Post model
class Post < ActiveRecord::Base
belongs_to :user
has_many :post_flags
has_many :flags, through: :post_flags
config/initializers/rails_admin.rb
config.model 'Post' do
exclude_fields :rank, :embedded_url, :attached_picture, :updated_at
object_label do
"#{bindings[:post].user.name}"
end
list do
field :text_content
field :created_at
field :user_id
field :likes_number
field :post_flags
end
end
I've tried adding "searchable" to the field :post_flags to no avail. Any help would be much appreciated.
I'm currently trying to work through a similar problem.
Adding the following to your rails admin config should allow you to add a post_flag filter:
field :post_flags do
searchable :name
end
However, you will get an SQL error when when you try to use the search filter.
We may have to hack RailsAdmin's MainController. Checkout this post: http://blog.endpoint.com/2013/07/hasmany-filter-in-railsadmin.html
when using rails_admin for associated objects (like has_and_belongs_to) it shows the ID of the object as the association.
This isn't a great deal for the users so I'ld like to change this for showing the text of the associated object.
Is this solvable?
Here a little example:
First Model:
class Menu
include Mongoid::Document
field :date, type: Date
has_and_belongs_to_many :meal
end
Second Model:
class Meal
include Mongoid::Document
field :text, type: String
has_and_belongs_to_many :menu
end
So it shows something like this:
But I'ld love to see the Text of the meals instead.
Simply define a title-method do the trick:
def title
self.text
end
You could use the RailsAdmin DSL object_label_method to change how the field is presented to the user.
In your case, something like this might do the trick:
RailsAdmin.config do |config|
config.model Menu do
list do
field :meal do
pretty_value do
value.text
end
end
end
end
end
I am completely new to ActiveAdmin and RoR and i cant figure out how to change the visible value of the dropdowns in a has_many association.
Fillup Model
class Fillup < ActiveRecord::Base
// key is car_id:integer
belongs_to :car
end
Car Model
class Car < ActiveRecord::Base
validates :description, :presence => true
key is fillup_id:integer
has_many :fillups
end
What it currently shows:
It currently shows im assuming an encoded reference to the Car assigned to it.
What i need it to show:
I need it to show the description given which is defined as description:string in the Car Model.
Something like this should work...
In app/admin/model_name.rb
form do |f|
f.inputs "My Model Name" do
# add your other inputs
f.input :cars, :collection => Car.all.map{ |car| [car.description, car.id] }
f.buttons
end
end
Read this article to learn more about modifying the form.
AciveAdmin uses formtastic, you should read about that as well.
In your Car model, just add something like :
def to_s
description
end
It should do the job !
Explanation : Actually, your Car's to_s method returns the object id corresponding to the current instance, that's the default thing used when using a method like puts on an object. To replace a model's display name, you have to override this method and it will work anywhere in your app when you use puts #car or in your templates doing <%= #car %>
I'd like to be able to filter an object based on an attribute of it's parent:
class Call < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :calls
end
I'd like to be able to do this:
ActiveAdmin.register Call do
filter :user
end
and have it filter on user.name, rather than present a select of all users. Can this be done?
Denis's solution almost worked for me. I just needed to add the filter type. For example:
ActiveAdmin.register Call do
filter :user_name, :as => :string
end
Try this:
ActiveAdmin.register Call do
filter :user_name
end
Since ActiveAdmin uses meta_search for filters their doc is very helpful: https://github.com/ernie/meta_search
In the next release of ActiveAdmin (I work with 1.0.0.pre) you can use Ransack methods. So, let say you have an Article, which belongs_to User.
You will have the following admin/article.rb file
ActiveAdmin.register Article do
controller do
def scoped_collection
Article.includes(:user)
end
end
index do
column :id
column :created_at
column :title
column("Author", sortable: 'users.first_name') { |item| link_to item.user.full_name, user_path(item.user) }
actions
end
filter :user_first_name_cont, :as => :string
filter :user_last_name_cont, :as => :string
end
Here, user_first_name_cont is ransack method which filters on associated user first_name and 'cont' means contains.
You can use nested resources from InheritedResource which is used by ActiveAdmin, so your list is automatically filtered by the parent.
ActiveAdmin.register User do
# this is the parent resource
end
ActiveAdmin.register Call do
belongs_to :user # nested below resource user
end
You can then use rake routes to see the new nested routes, generated by ActiveAdmin :) Hope it helps
I'd say it hugely depends on the types of associations you have between your models - it took me hours to figure this out.
Example
class User < ActiveRecord::Base
belongs_to :user
end
class Email < ActiveRecord::Base
has_one :email
end
To filter users based on their emails, you do this (don't forget the as: :string part as it gives you access to Ransack search methods such as contains and the likes)
ActiveAdmin.register User do
filter :user_email, :as => :string
end