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
Related
I have the following model
class Campaign < ActiveRecord::Base
has_many :campaign_rules
has_many :campaign_details
validates_presence_of [:start_at, :end_at]
rails_admin do
edit do
field :slug
field :start_at
field :end_at
field :is_active
fields :campaign_rules do
searchable :slug
end
fields :campaign_details
end
end
end
in my view, i get the following when i want to say create a new campaign.
But in my campaign_rules model, i have a field called slug which i would prefer to be shown as the default text in the associated record in the multi-select list. So for example for CampaignRule #1 , slug name is campaign-1 and i would prefer in my multi-select list to show campaign-1 instead of CampaignRule #1.
How can i do this?
I also want to be able to make sure that the multi-select dropdown list shown is based on associated campaign_id. Currently in my multi-select dropdown, it shows all records of the CampaignRule even though there is an association with campaign. How do i configure this as well?
By default rails admin uses the name attribute of an instance to display them.
You can tell rails admin what method to use adding this line on the initializer config file.
RailsAdmin.config {|c| c.label_methods << :rails_admin_title }
And then you would implement that instance method on your tag model
class CampaignRaule < ApplicationRecord
def rails_admin_title
self.slug
end
end
As to how customize the records on the multi-select dropdown you can filter them with a regular ActiveRecord scope like this:
class Campaign < ApplicationRecord
rails_admin do
edit do
field :campaign_rules do
associated_collection_scope do
associated_collection_scope do
campaign = bindings[:object]
proc { |scope| scope.where(campaign: campaign) }
end
end
end
end
end
end
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!
I am trying to change the navigation of the name in my rails admin dashboard.
This is what I have under my rails_admin.rb file
config.model 'Product' do
list do
field :id
field :design_code
field :brand
field :is_hidden
field :filename
end
Where am I suppose to change the name of navigations labels?
Basic label configuration for rails admin:
config.model 'Product' do
label 'Item' # Change the label of this model class
field :id
field :design_code
field :brand do
label 'Company' # Change the label of this field
end
field :is_hidden
field :filename
end
See the rails_admin wiki for more info.
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.
Using RailsAdmin. I have a Post model and a User model. Each post belongs to a user.
I use the following code to get RailsAdmin to handle the association for me and automatically set the user_id when a Post is created:
config.model Post do
edit do
field :user_id do
# use a form_field instead of the default drop-down
partial :form_field
# hide the input
view_helper :hidden_field
# set the value to current_user.id
def value
bindings[:view]._current_user.id
end
end
end
end
This code works, it sets the user_id to that of the current_user and it also hides the form_field (the html input) from view so that user is not even aware that it is being set on their behalf.
There is one small problem though. Whilst I'm able to hide the form_field, I can't hide it's associated label (i.e. the label that reads "User" which appears next to the input) - which means my users see this:
As you can see, there is a label "User" with an empty space next to it, and the word "Required" underneath.
Is there anyway to hide an input's associated label too (and not just the input itself) so that it's not confusing to the user? Is there something wrong with my code?
Thanks in advance
I think I've found a working solution:
field :user_id do
view_helper :hidden_field
# I added these next two lines to solve this
label ""
help ""
partial :form_field
def value
bindings[:view]._current_user.id
end
end
Not ideal, but it works
I also ran into the same obstacle and after some trial and error (and tips from the rails_admin group) arrived at a slight alternative:
config.model Library do
edit do
field :user_id do
# This hides the field label
label :hidden => true
# This hides the help field *yuk*
help ""
def value
bindings[:view]._current_user.id
end
# This hides the field input
view_helper do
:hidden_field
end
end
end
field :name
end
I think this feature is added to rails_admin already and no need a workaround solution for this. You can do it as the following
config.model Post do
edit do
field :user_id do
# This hides everything related with this field
hide
# You still need to set this value of course
def value
bindings[:view]._current_user.id
end
end
end
end