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.
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 have a User class that has_many Addresses.
When I am editing the user, and I click to add a new Address in RailsAdmin, I would expect it to build it in the context of that user (since I'm on its page), but it just gives me a dropdown of all users in my system to select from.
How can I get RailsAdmin to set the new Address object's user_id attribute to the id of the user that I am editing?
Here's my current config for the users edit action:
# this just pretty-prints the address names and also scopes the list down to this specific user
config.model 'User' do
configure :addresses, :has_many_association
edit do
configure :addresses do
associated_collection_scope do
user = bindings[:object]
proc { Address.where(user_id: user) }
end
end
end
end
# I'm guessing something has to get done in here
config.model 'Address' do
configure :user, :belongs_to_association
modal do
configure :user_id do
default_value do
# bindings[:object] is an empty Address in this context
end
end
end
end
The trick was to add accepts_nested_attributes_for :addresses to the User model.
That changed the edit view to just have a button to add new Addresses and edit the existing addresses related to the record.
No special configuration was necessary in the RailsAdmin initializer.
My app is a rails_admin with mongoid working well.
Now I'm trying to use nested attributes on edit form, but I don't know exactly how to do this.
My document is:
{
"_id" :ObjectId("..."),
"email" :"steve#steve.com",
"name" :"steve",
"facebook":{
"id":12345,
}
}
So, my model is:
class User
include Mongoid::Document
field :name
field :email
field :facebook
end
So, the problem is:
On the edit form it shows a text field with facebook json content: {"id":12345}
How can I use one text field for each sub attribute? I tried something like field :facebook.id but it doesn't work.
thanks!
Better is to make a custom field serialization of facebookHash, something similar to
class User
include Mongoid::Document
field :name
field :email
field :facebook, :type => facebookHash
end
If you did not have idea regarding custom field serialization, Have a look at Custom Fields in MongoId. Then you can easily create your fields partial on the basis of that model.
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
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