I use Active Admin gem for Ruby on Rails.
I have modules Team and Coach, which have a has_many and belongs_to relationship.
class Team < ActiveRecord::Base
belongs_to :coach
end
class Coach < ActiveRecord::Base
has_many :teams
end
I figured out how to display first name and last name on index and show page (i did it like that:)
index do
column :name
column "Coach" do |team|
team.coach.firstname + " " + team.coach.lastname
end
default_actions
end
What i want is how to display first name and last name of coach in Team form (new and edit page) in dropdown menu?
Please help me with this.
Can you try this
f.input :coach_name, :as => :select, :collection => Coach.all.map {|u| [u.firstname, u.id]}, :include_blank => false
I had the same problem. The edit page shows object instances in the select menu such as,
#<Coach:0x00eff180c85c8>
To solve it and access each instance's fields use this,
form do |f|
f.inputs "Coaches" do
f.input :name
f.input :coach, member_label: Proc.new { |c| "#{c.firstname} #{c.lastname}"
end
f.actions
end
ActiveAdmin uses Formtastic and its documentation has more examples.
This stackoverflow answer helped me get this solution.
Try this:
f.input :coach_name, :as => :select, :collection => Coach.all.map {|u| [u.firstname.to_s, u.id]}
Related
I'm working on a summer camp registration app. There are five sessions a day, and each session has a number of classes that a camper can pick from, based on their grade.
Trying to follow the railscast episode on Dynamic Select Menus, when a camper selects their grade, I want the selection of classes available per session to be filtered by the grade. (Ie, they pick 2nd grade.. all but the 2nd grade class options are filtered out.)
My first step was to get the grouped_collection_select working. However, when I load the page I get the following error.
undefined method `map' for #<Grade:0x007f93810f6600>
If anyone has any ideas, I'd appreciate it!
grade.rb
class Grade < ActiveRecord::Base
attr_accessible :name
has_many :classes
end
class.rb
class Class < ActiveRecord::Base
attr_accessible :fall_grade_id, :session_ids
validates_presence_of :fall_grade_id, :session_ids
belongs_to :grade
has_many :session_class_relationships
has_many :sessions, :through => :session_class_relationships
end
registration.rb
class Registration < AcriveRecord::Base
attr_accessible :fall_grade_id, :session_1_class_id
end
registrations/new.html.haml
...
= simple_form_for(#registration, :html => {:class => 'form-horizontal' }) do |f|
= f.input :fall_grade_id, :collection => 1..6, :prompt => 'Select..', :label => 'Grade'
= f.input :session_1_class_id, collection: #session_1_classes, as: :grouped_select, group_method: :grade
...
Your #session_1_classes variable seems to be Grade instance instead of Class relation. You probably need to do something like this:
#session_1_classes = (code_to_find_grade_instance).classes
in your controller.
I'm building a daily deal Rails app to learn RoR.
I am facing a problem for the past few hours : i can't get a model's attribute of an other associated model on active admin. Let me show you exactly the problem :
I have two models: Brand (i.e the brand of the deal) and Deal. A deal belongs to a Brand but a Brand can have many Deals.
models/deal.rb is like this:
class Deal < ActiveRecord::Base
belongs_to :brand
and we have models/brand.rb:
class Brand < ActiveRecord::Base
has_many :deals
attr_accessible :name
And i did the t.belongs_to in my migrations so this is ok.
In Active Admin's Deals' create form , i type, as admin, which brand the deal is associated with:
admin/game.rb
ActiveAdmin.register Deal do
# -- Form -----------------------------------------------------------
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :brand_id, :label => "Select a brand:", :as => :select, :collection => Brand.all
end
it works great, and i can create Deals with a certain brand.
but I CAN'T manage to display the NAME of the Brand in my list of Deals:
ActiveAdmin.register Deal do
index do
selectable_column
# id_column
column :title
column :deal_amount
column :brand do |deal|
link_to deal.brand.name
end
...doesn't work.
How can I do that ?
I tried everything but i basically don't know how to fetch the name of a Brand given it matches the brand_id in the Deal's table.
Any help appreciated.
show do |f|
panel "Subject" do
attributes_table_for f, :name, :description, :is_visible
end
panel "Pages in List View" do
table_for(f.pages) do |page|
column :name
column :permalink
column :is_visible
end
end
panel "Pages in View " do
div_for(f.pages) do |page|
panel page.name do
attributes_table_for page, :name, :description, :is_visible
end
end
end
end
end
You can do nested relations in same style as parent model
A couple things seem missing:
class Deal < ActiveRecord::Base
belongs_to :brands, foreign_key: :brand_id, class_name: 'Brand'
end
This is assuming that you mean partner to be a Brand and your schema uses brand_id for that relationship.
In your form, you can simply use:
form do |f|
f.inputs "Brand (i.e. client)" do
f.input :partner, label: 'Select a brand:'
end
end
Your link_to call won't actually link to a url the way you have it.
column :brand do |deal|
link_to deal.partner.name, admin_brand_path(deal.partner)
# or simpler
auto_link deal.partner
end
I would highly recommend trying to be consistent in your naming, as it will make things a lot less confusing and will require less code to make things work. i.e.
class Deal < ActiveRecord::Base
belongs_to :brand
end
f.input :brand, label: 'Select a brand:'
auto_link deal.brand
And your DB column can still be named brand_id.
So here's the Thing:
I have a Rails App with "Productos" and "Ventas" Both resources have the same attributes on their tables and ventas has one more (quantity)... the models look like this:
#Producto Model
class Producto < ActiveRecord::Base
has_and_belongs_to_many :categorias, :join_table => :categoria_productos
attr_accessible :color, :existencia, :nombre, :precio, :talla, :uniclave, :categoria_ids
#Venta Model
class Venta < ActiveRecord::Base
attr_accessible :cantidad, :color, :nombre, :precio, :talla, :uniclave, :producto_ids
has_many :productos
end
I'm using ActiveAdmin for the Admin interface and my /admin/venta.rb looks like this:
ActiveAdmin.register Venta do
form do |f|
f.inputs "Registrar Venta" do
f.input :cantidad
f.input :productos, :as => :check_boxes
end
f.buttons
end
end
The result is ALL THE PRODUCTOS are showing in the "new venta" form and I can select them, but when I create a new Venta actually, the params of "venta" save empty instead of taking the selected "producto" ones...
How can I fix this?? I want all the params of the selected "producto" to be used in the newly created "venta" fields as they share the same attributes (both models have been created with the same attributes actually)
So, ideas? ;)
:categoria_ids and : producto_ids must be as :categoria_id and :producto_id OR you must use the :foreign_key for behavior between models
I have the models Home and Photo, which have a has_many - belongs_to relationship (a polymorphic relationship, but I dont think that matters in this case). I am now setting up active admin and I would like admins to be able to add photos to homes from the homes form.
The photos are managed by the CarrierWave gem, which I dont know if will make the problem easier or harder.
How can I include form fields for a different model in the Active Admin Home form? Any experience doing something like this?
class Home < ActiveRecord::Base
validates :name, :presence => true,
:length => { :maximum => 100 }
validates :description, :presence => true
has_many :photos, :as => :photographable
end
class Photo < ActiveRecord::Base
belongs_to :photographable, :polymorphic => true
mount_uploader :image, ImageUploader
end
Try something like this in app/admin/home.rb:
form do |f|
f.inputs "Details" do
f.name
end
f.has_many :photos do |photo|
photo.inputs "Photos" do
photo.input :field_name
#repeat as necessary for all fields
end
end
end
Make sure to have this in your home model:
accepts_nested_attributes_for :photos
I modified this from another stack overflow question: How to use ActiveAdmin on models using has_many through association?
You could try this:
form do |f|
f.semantic_errors # shows errors on :base
f.inputs # builds an input field for every attribute
f.inputs 'Photos' do
f.has_many :photos, new_record: false do |p|
p.input :field_name
# or maybe even
p.input :id, label: 'Photo Name', as: :select, collection: Photo.all
end
end
f.actions # adds the 'Submit' and 'Cancel' buttons
end
Also, you can look at https://github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md (See Nested Resources)
I guess you are looking for a form for a nested model. Take a look at following railscasts.
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
I cannot tell you much about active_admin, but I think this should not make a difference in handling the nested model.
I have a has_one model, like this:
f.has_many :addresses do |a|
a.inputs "Address" do
a.input :street ... etc.
While this correctly reflects our associations for Address (which is a polymorphic model) using f.has_one fails. So I changed over to has_many and all's well. Except now we have to prevent our users from creating multiple addresses for the same entity.
We are using active_admin for our administration backend.
We have a model "App" that :belongs_to model "Publisher":
class App < ActiveRecord::Base
belongs_to :publisher
end
class Publisher < ActiveRecord::Base
has_many :apps
end
When creating a new entry for the "App" model I want to have the option to either select an existing publisher or (if the publisher is not yet created) to create a new publisher in the same (nested) form (or at least without leaving the page).
Is there a way to do this in active_admin?
Here's what we have so far (in admin/app.rb):
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :title
...
end
f.inputs do
f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
p.input :name
end
end
f.buttons
end
After hours of searching, I'd appreciate any hint... Thanks!
First, make sure that in your Publisher model you have the right permissions for the associated object:
class App < ActiveRecord::Base
attr_accessible :publisher_attributes
belongs_to :publisher
accepts_nested_attributes_for :publisher, reject_if: :all_blank
end
Then in your ActiveAdmin file:
form do |f|
f.inputs do
f.input :title
# ...
end
f.inputs do
# Output the collection to select from the existing publishers
f.input :publisher # It's that simple :)
# Then the form to create a new one
f.object.publisher.build # Needed to create the new instance
f.semantic_fields_for :publisher do |p|
p.input :name
end
end
f.buttons
end
I'm using a slightly different setup in my app (a has_and_belongs_to_many relationship instead), but I managed to get it working for me. Let me know if this code outputs any errors.
The form_builder class supports a method called has_many.
f.inputs do
f.has_many :publisher do |p|
p.input :name
end
end
That should do the job.
Update: I re-read your question and this only allows to add a new publisher, I am not sure how to have a select or create though.
According to ActiveAdmin: http://activeadmin.info/docs/5-forms.html
You just need to do as below:
f.input :publisher
I've found you need to do 3 things.
Add semantic fields for the form
f.semantic_fields_for :publisher do |j|
j.input :name
end
Add a nested_belongs_to statement to the controller
controller do
nested_belongs_to :publisher, optional: true
end
Update your permitted parameters on the controller to accept the parameters, using the keyword attributes
permit_params publisher_attributes:[:id, :name]