Activeadmin Formtastic custom input - ruby-on-rails

I have a simple app, which has three models Assessment, Question and AssessmentQuestion
In Assessment i have the association like,
class Assessment < ActiveRecord::Base
has_many :assessment_questions, dependent: :destroy
has_many :questions, through: :assessment_questions
end
In Question i have,
class Question < ActiveRecord::Base
has_many :assessment_questions, dependent: :destroy
has_many :bank_questions, dependent: :destroy
end
In AssessmentQuestion i have,
class AssessmentQuestion < ActiveRecord::Base
belongs_to :question
belongs_to :assessment
end
assessment_questions table has :assessment_id, :question_id and :mark columns
I have admin interface using ActiveAdmin gem.
While creating assessments in admin interface, In admin/assessment.rb i have a form generated by the formtastic gem,
form do |f|
f.inputs do
f.input :name
f.input :duration
f.input :questions, as: :check_boxes, member_label: :id
f.input :creator_id
end
f.actions :commit
end
This looks fine and no problem. The thing that i want is while choosing questions through the checkboxes, i want a textbox below or side to each checkbox containing mark of the questions will be filled in text boxes respectively ( through question.assessment_question.mark association ), so that i can edit the prefilled marks of the questions while creating assessment or leave it as it is.
I have tried, but got some error like
undefined method `to_sym' for {:for=>:questions}:Hash
My code,
form do |f|
f.inputs do
f.input :name
f.input :duration
f.input for: :questions do | question |
f.input :question, as: :select
f.input question.assessment_question.mark
end
f.input :creator_id
end
f.actions :commit
end
Any solutions?

Finally, i figured it out.
I actually created a custom semantic_form_for form _form.html.erb as a partial and included this in admin/assessment.rb file
form partial: 'assessment/form'
That's how i solved.
Reference: http://activeadmin.info/docs/5-forms.html

I would make a custom input that contains a checkbox and a string input:
https://github.com/justinfrench/formtastic#modified--custom-inputs

The solution is obvious, your "f.input :for" misses an "s".
It should be f.inputs

Related

ActiveAdmin model with has_many relationship - custom form button to "Add All" possible resources

Example models:
class Person
has_many :auto_mappings, dependent: :destroy
class Vehicle
has_many :auto_mappings
class AutoMapping
belongs_to :person
belongs_to :vehicle
validates :person,
:vehicle,
:usage,
presence: true
In my ActiveAdmin form for Create/Edit on Person, I include an input f.has_many for the nested AutoMapping resource to add a single AutoMapping at a time, which works great. What I would like to accomplish is to somehow add a button that would add an AutoMapping for each Vehicle to the Person.
Is there an approach to accomplish this or is there another direction I should go?
Example AA form for Person:
form do |f|
f.inputs 'Details' do
f.input :name
f.input :age
f.has_many :auto_mappings, class: 'auto_mapping' do |item|
item.input :vehicle, include_blank: false, input_html: {class: "vehicle", disabled: item.object.persisted?}
item.input :usage,
as: :select,
collection: %w[drive ride both]
include_blank: false
end
end
# Maybe a custom member action to "Create Person with all Auto Mappings"?
# I would prefer to auto populate the form page with `AutoMappings` rather than create them all on Submit if possible.
f.actions
end
Thanks!
You would have to extend has_many.es6 to make an Ajax call and add the items.

Can`t Update or Delete when using Active Admin

I would like to ask how to customize active admin.
I`m making my own blog and creating admin page using active admin gem.
This has many to many relationship with article and tag through article_tag table.
What I want to do is to add tag from article panel and I was able to show tag view in article panel, but it is not working fine.(I can`t update or remove tag after save once)
http://localhost:3000/admin/articles/new
image
I made the many to many relation with Article and Tag model like this.
article.rb
class Article < ActiveRecord::Base
has_many :article_tags
has_many :tags, through: :article_tags
accepts_nested_attributes_for :article_tags, :allow_destroy => true
end
tag.rb
class Tag < ActiveRecord::Base
has_many :article_tags
has_many :articles, through: :article_tags
end
article_tag.rb
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
And I customized active admin like this.
ActiveAdmin.register Article do
permit_params :title, :description, :url, :image_url, :media, article_tags_attributes: [:article_id, :tag_id, :name, :_destroy, :_edit]
form do |f|
# f.semantic_errors *f.object.errors.keys
f.inputs "Admin Details" do
f.input :title
f.input :description
f.input :url
f.input :image_url
f.input :media
f.input :publish
end
f.inputs "Articles" do
f.has_many :article_tags do |t|
t.input :tag
end
end
f.actions
end
end
But after I saved the article with tag once I can`t update tag or delete tag...
Does anyone know how to fix this?
You forgot to permit :id attribute of article_tag object. It is passed when updating/deleting existing nested object.
ActiveAdmin.register Article do
permit_params :title, :description, :url, :image_url, :media, article_tags_attributes: [:id, :article_id, :tag_id, :name, :_destroy, :_edit]
...
end

ActiveAdmin update two models in one form

I have an ActiveAdmin page to edit Loan info, like that:
ActiveAdmin.register Loan do
...
form do |f|
f.inputs 'Loan' do
f.input :name
f.input :amount
end
f.actions
end
(I'm omitting lot of fields for clarity)
Now, due to performance reasons I extracted amount field to LoanDetails model that has one-to-one relation with Loan
class Loan < ActiveRecord::Base
...
has_one :details, class_name: 'LoanDetails', foreign_key: :loan_id
...
end
class LoanDetails < ActiveRecord::Base
...
belongs_to :loan
...
end
This change brakes ActiveAdmin page. Attempt to edit loan results in
unknown attribute 'amount' for Loan.
error.
How can I update my ActiveAdmin to work properly with new data structure?
My colleague helped me to solve this while I was in the middle of writing StackOverflow question.
ActiveAdmin.register Loan do
...
form do |f|
f.inputs 'Loan' do
f.input :name
end
f.inputs 'Loan Details', for: [:details, f.object.details] do |d|
d.input :amount
end
f.actions
end
end

Active Admin has_many selectable list of records

I have been trying for days now, I am new at ROR and active admin. So far I have been able to add and delete has_many relations for a new record. And I am using strong_parameters as well as accept_nested_attributes. I want the
Ability to add and delete relations for existing records as well.
Ideally there should be an autocomplete box that allows searching and selection of an existing meaning for this particular model.
My models are
Word
Meaning
WordMeaning
I only want the capability to attach meanings that are already available to a word?
class Word < ActiveRecord::Base
belongs_to :language
has_many :word_meanings
has_many :meanings ,through: :word_meanings
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :language
f.input :word
f.input :wordInScript
f.input :pronunciation, :required => false, :as => :file
end
f.inputs do
f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
a.input :meaning
a.input :language
end
end
f.actions
end
You can determine the collection of the select:
a.input :meaning, :as => :select, :collection => {#your collection in Hash, Array or ActiveRecord relation}.map{|key, value| [value, key] }
ActiveAdmin uses Formtastic:
https://github.com/justinfrench/formtastic#usage

Rails 3 file upload with Carrierwave and Formtastic

I have a Product with many Photos. The relationship is this way:
product.rb
class Product < ActiveRecord::Base
has_many :photos, :dependent => :destroy
attr_accessible :translations_attributes, :active, :category_id, :photos
accepts_nested_attributes_for :translations, :photos
photo.rb
class Photo < ActiveRecord::Base
attr_accessible :photo, :product_id
belongs_to :attachable
mount_uploader :photo, PhotoUploader
and the form looks like this:
form(:html => { :multipart => true }) do |f|
f.inputs "Details" do
f.input :active
f.input :category, :include_blank => false
end
f.inputs "Photos" do
f.input :photos, :as => :file
end
f.buttons
end
The problem is that I get this error when creating/updating the product with a file attached:
undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x007f93c8549828>
I found a question with the same error here, but even trying their solution still have the same problem.
I'm using Friendly_Id, ActiveAdmin, Globalize3, maybe it could have some relationship with them, this is my first project with Rails and right now I don't know what could be...
Your problem is that you're declaring photos as a has_many association in your model, and declaring it as a single file input in your view. Many or one, take your pick :)
What you're probably trying to do is have photos as a nested model in your Product form - something like so:
semantic_form_for #product do |f|
f.semantic_fields_for :photos do |ff|
ff.input :photo, as: :file
To loop over each of the photos in your product, and display a file input field for each.

Resources