I have the relation ContentRelease has_many pages. The following activeadmin registered model lets me edit properties of a page inside the content_release, but not create new pages for the content release. There is no error, I get a success message, but a new page is not saved. What am I doing wrong?
ActiveAdmin.register ContentRelease do
form do |f|
f.actions
f.inputs "Details" do
f.input :active_at
end
f.inputs "Pages", :class => 'pages-list' do
f.has_many :pages do |page|
page.inputs "Associated Look" do
page.input :title
page.input :content_release, :as => :select, :collection => ContentRelease.list
page.input :look, :as => :select, :collection => Look.current
page.input :share_url
end
end
end
f.inputs "Looks", :class => 'looks-list' do
f.has_many :looks do |look|
look.inputs do
look.input :title
end
end
end
f.actions
end
Related
I am using the activeadmin gem with formtastic I want to add a datalist to a particular text field.
#FILE app/admin/model.rb
ActiveAdmin.register Model do
permit_params :title, :article_headline_a_id, :article_headline_b_id
form(html: { multipart: true }) do |f|
f.input :article_headline_a_id, :as => :datalist, :collection => Article.pluck(:title)
f.input :article_headline_b_id, :as => :datalist, :collection => Article.pluck(:title)
f.actions
end
end
Everything seems to work fine until I save and check the console I get article_a_id = 0 instead of the expected id number.
#model.rb
belongs_to :article1, :foreign_key => 'article_headline_a_id', :class_name => "Article"
belongs_to :article2, :foreign_key => 'article_headline_b_id', :class_name => "Article"
Sounds like you're not mapping the ID to the title try:
f.input :article_headline_a_id, :as => :datalist, :collection => Article.pluck(:title, :id)
I have two questionaires.rb like this:
ActiveAdmin.register Questionaire do
menu :label => proc{ I18n.t("menu.admin.questionaire") }, :priority => 25
# actions :all, :except => [:create, :destroy]
filter :role
filter :name
config.comments = false
scope :all
def new
create!
end
index do
column :id
column :name
column :role
column :description
column do |ques|
link_to t("activeadmin.questionaire.add_question"), new_admin_question_path
end
column do |ques|
link_to t("activeadmin.questionaire.show_question"), admin_questions_path
end
default_actions
end
form do |f|
f.inputs :questionaire do
f.input :id, :as => :hidden
f.input :name
f.input :role
f.input :description
end
f.actions
end
end
and one questionaire has_many questions, and questions belong_to questionaire.
and in the questions table,I add this:
t.references :questionaire
add_index :questions, :questionaire_id
But when I click the link:
link_to t("activeadmin.questionaire.add_question"), new_admin_question_path
the questionaire_id is blank!
How can I get the questionaire_id, and when click each link of each questionaire to add questions, these questions just belong to their questionaire?
edit:
the results of questions and questionaires after rake routes:
batch_action_admin_questionaires POST /admin/questionaires/batch_action(.:format) admin/questionaires#batch_action
admin_questionaires GET /admin/questionaires(.:format) admin/questionaires#index
POST /admin/questionaires(.:format) admin/questionaires#create
new_admin_questionaire GET /admin/questionaires/new(.:format) admin/questionaires#new
edit_admin_questionaire GET /admin/questionaires/:id/edit(.:format) admin/questionaires#edit
admin_questionaire GET /admin/questionaires/:id(.:format) admin/questionaires#show
PUT /admin/questionaires/:id(.:format) admin/questionaires#update
DELETE /admin/questionaires/:id(.:format) admin/questionaires#destroy
batch_action_admin_questions POST /admin/questions/batch_action(.:format) admin/questions#batch_action
admin_questions GET /admin/questions(.:format) admin/questions#index
POST /admin/questions(.:format) admin/questions#create
new_admin_question GET /admin/questions/new(.:format) admin/questions#new
edit_admin_question GET /admin/questions/:id/edit(.:format) admin/questions#edit
admin_question GET /admin/questions/:id(.:format) admin/questions#show
PUT /admin/questions/:id(.:format) admin/questions#update
DELETE /admin/questions/:id(.:format) admin/questions#destroy
I have two models: Asssessment and Question which are organized like this:
class Question < ActiveRecord::Base
belongs_to :assessment
class Assessment < ActiveRecord::Base
has_many :questions
I'm trying to create an activeadmin (ver 1.0.0) interface to create assessments and add questions to them.
So far I've tried making a questions tab:
ActiveAdmin.register Question do
permit_params :question_text, :question_type, :scale_min, :scale_max
form do |f|
f.inputs "Question Information" do
f.input :assessment, :as => :select, :collection => Assessment.non_daily_assessments
f.input :question_type, :as => :select, :collection => Question.human_readable_question_types.keys
f.input :question_text, :input_html => {:rows => 2, :cols => 10}
f.input :scale_min
f.input :scale_max
end
f.actions
end
non_daily_assessments simply returns a subset of all assessments
I am able to select from a list of assessments, but when I save the question and am taken to the "view question" page the question's assessment_id is empty.
Similarly, if I create an assessments tab:
ActiveAdmin.register Assessment do
permit_params :name, :questions
form do |f|
f.inputs "Assessment Information" do
f.input :name, :input_html => {:rows => 1, :cols => 10}
f.has_many :questions, :allow_destroy => true, :heading => 'Questions' do |qf|
qf.input :question_type, :as => :select, :collection => Question.human_readable_question_types.keys
qf.input :question_text, :input_html => {:rows => 2, :cols => 10}
qf.input :scale_min
qf.input :scale_max
end
end
f.actions
end
I am able to go to a particular assessment and start adding questions, but when I reload the page they're gone. Going into the console I see that the questions were created, but their assessment_id's are nil just like through the question tab.
What is a correct way to create an activeadmin interface for a belongs_to has_many relationship?
Let me know if you need more information.
Your permit_params are incomplete. Have a look at this answer: Nested form in activeadmin not saving updates
You need to add :assessment_id to the permit_params in the Question section, and if you want to be able to edit questions with the assessments, you're probably missing the accepts_nested_attributes_for :questions in the Assessment model, and you'll also need to change the permit_params in the Assessment section to something like
permit_params :name, questions_attributes: [:id, :question_type, :question_text, :scale_min, :scale_max]
I have a infrastructure object composed for many datacenters. In the apps/admin/infrastructures.rb I have the following code:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
I can add datacenters with no problems but I don't know how I can delete it from infrastructure form.
Sep 2017 Update:
Rails 5.1.4, ActiveAdmin 1.0.0
Append :id and _destroy in permit_params along with other attributes from the model e.g. :name in your case. Then provide the :allow_destroy option in f.has_many too. Other requirements remain the same; like adding allow_destroy: true in accepts_nested_attributes_for.
Final look:
ActiveAdmin.register Infrastructure do
permit_params :name, datacenters_attributes: [:id, :_destroy, :name]
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters, heading: false,
allow_destroy: true,
new_record: false do |datacenter_form|
datacenter_form.input :name
end
end
f.buttons
end
end
ActiveAdmin Reference
This worked for me:
i.input :_destroy, as: :boolean
and in the Model remember to add :allow_destroy :
accepts_nested_attributes_for :images, allow_destroy: true
Solved adding the following line:
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
The code looks like:
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.input :name
datacenter_form.input :_destroy, :as => :boolean, :required => false, :label => 'Remove'
end
end
f.buttons
end
If you cant destroy the object nested. You need to put :_destroy in your app/admin/object.rb permit_params
permit_params :id,:name, :cod, :_destroy
I hope this will be helpful (I've changed my code to suit your example, so I hope there are no typos here):
form do |f|
f.inputs "Infrastructure details" do
f.input :name
f.has_many :datacenters do |datacenter_form|
datacenter_form.inputs :datacenters do
datacenter_form.input :name
end
datacenter_form.buttons do
link_to "Delete", admin_datacenter_path(datacenter_form.object), method: "delete", class: "button" unless datacenter_form.object.new_record?
end
end
end
f.buttons
end
and the controller method should be defined in datacenters.rb
controller do
def destroy
#datacenter = Datacenter.find(params[:id])
#datacenter.destroy
redirect_to edit_admin_retailer_path(#datacenter.infrastructure)
end
end
This should work:
datacenter_form.label :_delete
datacenter_form.check_box :_delete
This adds a checkbox for each nested object which will delete the object if checked.
Don't forget to add the following to your parent model
has_many :child_name, :dependent => :destroy
I'm using ActiveAdmin and Formtastic.
I have an invoice form that has a drop down menu of shipments.
form do |f|
f.inputs "Shipment Details" do
f.input :shipment_id, :label => "Shipment", :as => :select, :collection => Shipment.find(invoiceless_shipments, :order => "file_number", :select => "id, file_number").map{|v| [v.file_number, v.id] }
f.input :issued_at, :label => "Date", :as => :datepicker
... more fields ...
end
I only want to display the select menu for shipments if the form is a New Invoice form.
I do not want to display the shipments drop down select menu if the form is an edit form. So if the form is an edit form, it won't be changed.
I was thinking about doing something like
if params[:action] != 'edit'
f.input :shipment_id, :label => "Shipment", :as => :select...
end
but I get a DSL error.
try
form do |f|
f.inputs "Shipment Details" do
if f.object.new_record?
f.input :shipment_id, :label => "Shipment", :as => :select...
end
...
end
end
Question (partially) answered earlier here: Accessing object of form in formtastic