I have a list of products and i have a supplier associated with the product. So when you add the product select the supplier from a dropdown. But If the supplier doesn't exist, i want the ability for the user to add a new supplier via a + icon next to the dropdown box which opens a new tab with suppliers/new. I've looked into the select2 addon, but it doesn't give me what I need.
https://select2.org/tagging
Please look at the select2's dynamic option creation. However documentation is hard to understand.
May be you can think of using modals like this type of situations.
I solved this with the activeadmin_addons gem. It turns select fields into select2 by default and with the tags option you can enable the creation of new options:
app/admin/products.rb
form do |f|
f.input :supplier, tags: true
end
I would've expected Select2 to send a create request when selecting a new option, however, it will send the name of the new option in the supplier_id param. Nevertheless we can work with that:
app/admin/products.rb
controller do
def create_supplier
return unless params.dig('product', 'supplier_id').is_a?(String)
params['product']['supplier_id'] = Supplier.create!(name: params['product']['supplier_id']).id
end
def update
create_supplier
super
end
def create
create_supplier
super
end
end
Related
When I'm going to create a new user, rails admin shows me many fields like created_at, sign_in_at, etc, etc etc.
You need to use create instead of list in the config block.
Enter the following code in config/initiazlizers/rails_admin.rb.
See https://github.com/sferik/rails_admin/wiki/Fields for further details regarding the customization of fields.
config.model 'User' do
create do
field :name
field :email
# etc....
end
end
I am using ActiveAdmin and i want to list items which belong to a specific user. The two resources have the has_many and belongs_to relationship.
An index pages is listing all the users. Now i would like to render a show block for each user his items.
My show looks now something like this:
ActiveAdmin.register User do
show do
panel "Specific Item List" do
table_for Item.where("user_id=1").fnidi_each do |i|
column("ID"){|item|item.id}
column("Name"){|item|item.name}
end
end
end
end
How do i inherit the user_id from the page to the show panel ? So that each time show is called i can use the users id for the query.
I know these a basic question but my knowledge of Rails/AA is so far quite basic as well ;) Happy for any advice.
Looking at this code from the ActiveAdmin documentation, where "post" seems to be dynamically generated within the register block suggests that in your case you may be able to just do "user.id", etc.
ActiveAdmin.register Post do
show do
h3 post.title
div do
simple_format post.body
end
end
end
So you might try user.items or Item.where(user_id: user.id) instead of your Item.where("user_id=1").
I have an extra position attribute on my many-to-many link table. Its purpose is to define hand-ordered position of an Item in a Group. The link model is called ItemGroupMembership.
I am allowing the user to edit this by jQuery UI sortables in a Backbone application.
What is the correct way to update this attribute. Do I treat the link table like a regular model and have a item_group_memberships_controller?
How are Item's assigned to Groups in the first place?
This should be done in the item_group_memberships_controller.
So,
class ItemGroupMembershipsController < Applicationontroller
def create
#create the relationship, passing in :position
end
def update
#update the relationship by updating position
end
end
Is ther a way to add a virtual model? Something such as a second dashboard where you can display more than one different resources?
I ask because I need to create a page with nothing but links to documents, like customer invoices, corporate invoices and other documents from other models.
The only way I know is to create some empty model:
app/models/fake.rb:
class Fake
end
then create generator app/models/fake.rb:
ActiveAdmin.register Fake do
config.comments = false
config.clear_sidebar_sections!
config.clear_action_items!
collection_action :index do
# here you can set you template
render 'you_template', :layout => 'active_admin'
end
end
The last changes in the master branch of ActiveAdmin deprecates the Dashboard and creates the concept of custom pages. See this .
I'm trying to include a list of data from a database table in a different model.
I have an Event model and a Venue model.
In the new Venue view I'd like to be able to include the list of existing venues to attach to an event so that administrators can link new event to existing venues and if I can manage it also allow for the creation of a new venue.
Is it in anyway possible?
When I try to get the #venue keys from the Event new view it just fails.
In the controller for that action you need to set #venues before you can use it in the view.
#venues = Venue.all
Looks like this what I need
http://railscasts.com/episodes/196-nested-model-form-part-1