user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :user_groups
end
Similarly, user_group.rb
class UserGroup < ActiveRecord::Base
has_and_belongs_to_many :users
end
Now the active admin page for user groups is as shown below:
ActiveAdmin.register UserGroup, as: 'UserGroup' do
form do |f|
f.inputs do
f.has_many :usergroup_users, as: 'member user' do |user|
user.input :user_id
end
end
end
end
Now this will create the user group form with fields for adding users to the new group. The button for adding new user to the group will be Add new User Group User. I want to modify this button label to something else. How can it be implemented?
Part of my problem is solved when using:
f.has_many :usergroup_users, heading: 'Add User to Group', as: 'member user' do |user|
...
end
This will change the header part to the required text. But the button label is still be a problem.
f.has_many :usergroup_users, new_record: 'Add member user' do |user|
...
end
The new_record attribute can be used to customize the button label.
In this way, one can change the text on new and edit pages buttons.
form do |f|
f.inputs do
f.input :email
f.input :phone
end
f.actions do
if resource.persisted?
# edit
f.action :submit, as: :button, label: 'Update Model'
else
# new
f.action :submit, as: :button, label: 'Create Model'
end
f.action :cancel, as: :link, label: 'Cancel'
end
end
I'm still a beginner with rails but have you tried this?
user.input :user_id, :input_html => { :value => 'your text' }
Related
I'm using Ruby on Rails 5 API app with Active Admin.
I have a model called Subscription with has_many courses (Course is also a model).
When I add courses to a subscription, I need to do it one by one, like in this picture:
What I want is to have an option to add all the courses to a subscription all at once, instead of one by one. I tried using checkbox for multiple selection but it didn't worked. This is the code:
ActiveAdmin.register Subscription do
# require 'lib/app_languages.rb'
permit_params :name, :seat_limit, :domain, :language, :organization, course_ids: [], user_ids: [], subscription_courses_attributes: [:id, :course_id, :_destroy, :_create, :_update]
config.sort_order = 'id_asc'
# Index
index do
id_column
column :name
column :seat_limit
column :domain
column :organization
column :language do |subscription|
AppLanguages.languages[subscription.language]
end
column "Unlocked courses", :courses
column :created_at
actions
end
# Show
show do
attributes_table do
row :name
row :language do |subscription|
AppLanguages.languages[subscription.language]
end
row :seat_limit
row :domain
row :organization
row :created_at
end
panel 'Unlocked Courses' do
table_for subscription.courses do
column :id
column "Title" do |course|
link_to course.title, admin_course_path(course)
end
column "language" do |course|
AppLanguages.languages[course.language]
end
column :author
end
end
panel 'Subscribed users' do
table_for subscription.users do
column :id
column "name" do |user|
link_to user.name, admin_user_path(user)
end
column :email
column "language preference" do |user|
AppLanguages.languages[user.language]
end
end
end
end
# Edit
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs 'Details' do
f.input :name
f.input :language, :as => :select, :collection => AppLanguages.languages_array
f.input :seat_limit
f.input :domain
f.input :organization
end
f.has_many :subscription_courses do |sub_c|
sub_c.inputs "Unlocked Courses" do
if !sub_c.object.nil?
sub_c.input :_destroy, as: :boolean, label: "Destroy?"
end
sub_c.input :course ### should add here option for add all
end
end
f.inputs 'Subscribed users:' do
f.input :users
end
f.actions
end
end
Using for the array the as: :select form input with multi-select should help. Something like this:
sub_c.input :items, as: :select,
collection: courses_collection,
multiple: true
You can try using check_boxes
sub_c.input :items, as: :check_boxes, collection: courses_collection
In my application I have 2 models: AdminUser, which has_many :announcements, and Announcement, which belongs_to :admin_user. The Announcement table has admin_user_id column in a database.
In app/admin/announcement.rb I have:
ActiveAdmin.register Announcement do
permit_params :title, :body, :admin_user_id
controller do
def new
#announcement = Announcement.new
#announcement.admin_user_id = current_admin_user.id
end
end
form do |f|
f.inputs do
f.input :title
f.input :body, as: :ckeditor
end
f.actions
end
end
Why my controller doesn't work? When I create an announcement through activeadmin, the column admin_user_id is empty. How can I solve this issue?
Just added to a form: f.input :admin_user_id, as: :hidden, and now everything is working.
I have nested resources in active admin edit page, but I would like only to allow admin to edit content of existing resources, not to add new nested resources. My code looks like that:
form do |f|
f.inputs do
f.input :author
f.input :content
f.has_many :comments do |comment_form|
comment_form.input :content
comment_form.input :_destroy, as: :boolean, required: false, label: 'Remove'
end
end
f.actions
end
But it add's "Add new comment" button under inputs. How can I disable it, and leave only f.actions buttons for main form?
Starting from v0.6.1 you can pass new_record: false to hide the "Add new" button
f.has_many :comments, new_record: false do |comment_form|
...
end
The commit 4b58b8
I'm using Active Admin and I have a one-to-many relationship between two models:
class WeeklyMenu < ActiveRecord::Base
has_many :menu_items
attr_accessible :menu_items
accepts_nested_attributes_for :menu_items
end
In the admin page for WeeklyMenu I'd like to display five menu_items. This is what my admin page looks like at the moment:
ActiveAdmin.register WeeklyMenu do
form do |f|
f.inputs "Details" do
f.input :title
f.input :week
end
f.has_many :menu_items do |menu_item|
menu_item.input :title
end
f.buttons
end
end
That gives me a very nice interface for adding more menu_items, but I want the user to have five of them - no more, no less. How would I go about doing that?
Replace
f.has_many :menu_items do |menu_item|
menu_item.input :title
end
with
f.inputs "Menu items" do
5.times do
f.object.menu_items.build
end
f.fields_for :menu_items do |m|
m.inputs do
m.input :title
end
end
end
May not be the best solution, but this should work...
The fields_for answer that #user946611 suggested didn't work for me, but the following code did:
f.inputs 'Menu Items' do
(5 - f.object.menu_items.count).times do
f.object.menu_items.build
end
f.has_many :menu_items, new_record: false do |m|
m.input :title
m.input(:_destroy, as: :boolean, required: false, label: 'Remove') if m.object.persisted?
end
end
That will always show 5 forms for items, whether they have that many created or not. The new_record: false disables the "Add New Menu Item" button.
If you want to edit the form again, the answer of #user946611 lacks of a condition to tell whether the menu_item exists, because when submit and edit the form there will generate another five menu_items.
so it should be:
f.inputs 'Menu Items' do
if !f.object.menu_items.present?
5.times do
f.object.menu_items.build
end
end
f.fields_for :menu_items do |m|
m.inputs do
m.input :title
end
end
end
Activeadmin defines callbacks that can be used for this sort of thing:
https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/resource_dsl.rb#L157-L161
The after_build hook seems like an appropriate place to initialize a has_many relationship
ActiveAdmin.register WeeklyMenu do
after_build do |weekly_menu|
(5 - weekly_menu.menu_items.size).times do
weekly_menu.menu_items.build
end
end
form do |f|
f.inputs "Details" do
f.input :title
f.input :week
end
f.has_many :menu_items do |menu_item|
menu_item.input :title
end
f.buttons
end
end
This is similar to my previous question Ruby On Rails Active Admin has_many changing dropdown to use a different column
I figured out how to reassign a f.inputs but how would i go about reassigning the display of the data when viewing an item...
e.g:
Public Git Repo: https://github.com/gorelative/TestApp
Snippet of code i have in my fillups.rb
ActiveAdmin.register Fillup do
form do |f|
f.inputs do
f.input :car, :collection => Car.all.map{ |car| [car.description, car.id] }
f.input :comment
f.input :cost
f.input :mileage
f.input :gallons
f.buttons
end
end
end
Modify the show action
ActiveAdmin.register Fillup do
# ... other stuff
show do
attributes_table do
# add your other rows
row :id
row :car do |fillup|
link_to fillup.car.description, admin_car_path(fillup.car)
end
end
end
end