I am making a blog using rails and activeadmin. I associate tags to each post. I want to know how to configure activeadmin to add tags on posts.
I want to be able to create tags from a new Post form. How can i do that?
Thank you
if Posts has_many Tags then something like this could work.
form do |f|
f.inputs do
f.input :title
f.input :content
f.has_many :tags do |fu|
fu.input :name
fu.input :_destroy, :as=>:boolean, :required => false, :label => 'Delete Tag' unless fu.object.new_record?
end
end
end
ActiveAdmin expects that the Post model accepts_nested_attributes_for :tags
like..
class Post
#....
accepts_nested_attributes_for :tags
end
Related
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
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
Is there anyway to generate Active Admin's views? I know how to override them but I'd like to keep their basic layout but just add some nested forms.
According to Active Admin Documentation, there is no way to generate all views:
You can create forms with nested models using the has_many method:
ActiveAdmin.register Post do
form do |f|
f.inputs "Details" do
f.input :title
f.input :published_at, :label => "Publish Post At"
end
f.inputs "Content" do
f.input :body
end
f.inputs do
f.has_many :categories, :allow_destroy => true, :heading => 'Themes', :new_record => false do |cf|
cf.input :title
end
end
f.actions
end
end
The :allow_destroy option will add a checkbox to the end of the nested form allowing removal of the child object upon submission. Be sure to set :allow_destroy => true on the association to use this option.
The :heading option will add a custom heading to has_many form. You can hide a heading by setting :heading => false.
The :new_record option will show or hide new record link at the bottom of has_many form. It is set as true by default.
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
In my Rails application, I have the following model:
class Idea < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :ideas
end
I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:
form do |f|
f.inputs do
f.input :member
f.input :description
end
end
The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?
Any help will be appreciated.
Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.
form do |f|
f.inputs do
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
f.input :description
end
end
For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.
For Example:
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h
collection: [{name1: 1}, {name2: 2}, {name3: 3}]
Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.