unnecessary text display in active admin "awesome nested set" gem in ROR - ruby-on-rails

I am using awesome nested set gem in ROR and it's working fine, But in add form, unnecessary text is displaying.
#<#<Class:0x00000006fa4c30>:0x00000006f7fe30>
here is my code
Controller
ActiveAdmin.register Category do
permit_params :name, :lft, :rgt, :parent_id, :depth
# Set the default sort order.
config.sort_order = 'lft_asc'
# Add member actions for positioning.
sortable_tree_member_actions
form do |f|
f.inputs do
f.input :parent, :as => :select, :collection => f.template.nested_set_options(Category, #category) {|i| "#{'--' * i.level} #{i.name}" }
#f.input :parent, :as => :select, :collection => Category.where("parent_id IS NULL")
f.input :name
end # f.inputs
f.actions
end # form
index do
# This adds columns for moving up, down, top and bottom.
sortable_tree_columns
sortable_tree_indented_column :name
actions
end
end
Model
class Category < ActiveRecord::Base
# awesome nested set
acts_as_nested_set
validates :name, :presence => true
#default_scope :order => 'lft ASC'
#...
end

f.input :parent, :as => :select, :collection => nested_set_options(Category, #category) {|i| "#{'--' * i.level} #{i.name}" }
remove f.template and it won't print that

Related

has_one relation in form in active admin

I have two models/tabels: room and room_location, that have a one on one relation:
class Room < ApplicationRecord
has_one :room_location
end
class RoomLocation < ApplicationRecord
belongs_to :room
end
And this is what i want to do in my form in rooms.rb:
ActiveAdmin.register Room do
menu parent: 'Rooms'
permit_params :name, :description, room_location_attributes: [:address, :city]
form do |f|
f.inputs 'Roomsdata' do
f.input :name, as: :string
f.input :description
f.has_one :room_location do |t|
t.inputs do
t.address
t.city
end
end
f.actions
end
end
end
The has_one doesnt work and if i do has_many, it says relation "room_locations" does not exist
You should write in the params room_location_id instead of attributes
ActiveAdmin.register Room do
menu parent: 'Rooms'
permit_params :name, :description, room_location_id
form do |f|
address_id = ''
cs = params[:id].present? ? Case.find(params[:id]) : nil
unless cs.nil?
address_id = cs.address.id unless cs.address.nil?
end
f.inputs 'Roomsdata' do
f.input :name, as: :string
f.input :description
f.input :room_location_id , :as => :select, :collection => room_location.order(address: :desc).to_a.uniq(&:address).collect {|room_location| [room_location.address, room_location.id] }, selected: room_location_id
f.input :room_location_id , :as => :select, :collection => room_location.order(city: :desc).to_a.uniq(&:city).collect {|room_location| [room_location.address, room_location.id] }, selected: room_location_id
f.actions
end
end
end

Formtastic Datalist Activeadmin not saving

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)

belongs_to id not being set activeadmin

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]

What is the best way of creating categories in Rails 4?

I am trying different methods of creating categories in rails 4 to just see what works the best, so far I am not having much joy. I do not want to use a gem to create this as thats not the answer.
I have a set of events that I want to categorize.
Can anyone suggest the best and most convenient way of getting this right?
Thanks
We've created a set of categories for our system recently, and was relatively simple. Just use a has_and_belongs_to_many:
#app/models/category.rb
Class Category < ActiveRecord::Base
has_and_belongs_to_many :events
end
#app/models/event.rb
Class Event < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Schemas:
categories
id | name | created_at | updated_at
events
id | name | created_at | updated_at
categories_events
category_id | event_id
This will allow you to call things like this:
#app/controllers/events_controller.rb
def add_category
#event = Event.find(params[:id])
#category = Category.find(params[:category_id])
#event.categories << #category #->> as to be two ActiveRecord objects
end
Heres my code update
admin/category.rb
ActiveAdmin.register Category do
controller do
def permitted_params
params.permit category: [:name]
end
def find_resource
scoped_collection.friendly.find(params[:name])
end
end
form do |f|
f.inputs "Details" do
f.input :name
end
f.actions
end
end
models/category.rb
class Category < ActiveRecord::Base
has_and_belongs_to_many :events
end
admin/event.rb
ActiveAdmin.register Event do
controller do
def permitted_params
params.permit event: [:title, :slug, :event_image, :category, :eventdate, :description]
end
def find_resource
scoped_collection.friendly.find(params[:id])
end
def index
#events = current_category.events
end
end
form do |f|
f.inputs "Details" do
f.input :title
end
f.inputs "Event Date" do
f.input :eventdate, :date_select => true, :as => :date_picker, :use_month_names => true
end
f.inputs "Category" do
f.input :categories, :as => :select, :collection => Category.all
end
f.inputs "Biography" do
f.input :description, :as => :ckeditor, :label => false, :input_html => { :ckeditor => { :toolbar => 'Full', :height => 400 } }
end
f.inputs "Image" do
f.file_field :event_image
end
f.actions
end
end
model/event.rb
class Event < ActiveRecord::Base
has_and_belongs_to_many :categories
has_attached_file :event_image, styles: {
large: "600x450#",
medium: "250x250#",
small: "100x100#"
}, :default_url => "/images/:style/filler.png"
validates_attachment_content_type :event_image, :content_type => /\Aimage\/.*\Z/
validates :title, :slug, :event_image, presence: true
extend FriendlyId
friendly_id :title, use: :slugged
end

Nested form in activeadmin not saving updates

I have a nested form in ActiveAdmin for these models (a :class_section has_many :class_dates):
class ClassDate < ActiveRecord::Base
belongs_to :class_section
validates :start_time, :presence => true
validates :end_time, :presence => true
end
and
class ClassSection < ActiveRecord::Base
belongs_to :class_course
has_many :class_dates
belongs_to :location
accepts_nested_attributes_for :class_dates
end
Everything seems to be in the right place when I look at the form. However, when I update a class_date, it doesn't save.
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id
form do |f|
f.inputs "Details" do
f.input :class_course, member_label: :id_num
f.input :min_students, label: "Minunum Students"
f.input :max_students, label: "Maxiumum Students"
f.input :location
end
f.inputs do
f.input :info, label: "Additional Information"
end
f.inputs "Dates" do
f.has_many :class_dates, heading: false do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
index do
column :class_course
column :location
default_actions
end
filter :class_course
filter :location
show do |cs|
attributes_table do
row :class_course do
cs.class_course.id_num + " - " + cs.class_course.name
end
row :location
row :min_students, label: "Minunum Students"
row :max_students, label: "Maxiumum Students"
row :info, label: "Additional Information"
end
panel "Dates" do
attributes_table_for class_section.class_dates do
rows :start_time, :end_time
end
end
active_admin_comments
end
end
Here is the ActiveAdmin file for ClassDates:
ActiveAdmin.register ClassDate, as: "Dates" do
permit_params :start_time, :end_time, :class_section_id
belongs_to :class_section
end
Can you see a reason why it's not saving properly?
UPDATE: I added the following code to the AA and it seems to work now:
controller do
def permitted_params
params.permit!
end
end
Let me know if there is a better solution. Thanks.
UPDATE 2: There is one lingering problem however. I am unable to delete ClassDates using this form.
You need to permit nested parameters, but you should never use params.permit!. It's extremely unsafe. Try this:
ActiveAdmin.register ClassSection do
permit_params :max_students, :min_students, :info, :class_course_id, :location_id,
class_dates_attributes: [ :id, :start_time, :end_time, :_destroy ]
form do |f|
# ...
f.inputs "Dates" do
f.has_many :class_dates, heading: false, allow_destroy: true do |cd|
cd.input :start_time, :as => :datetime_picker
cd.input :end_time, :as => :datetime_picker
end
end
f.actions
end
# ...
end
The configuration (and permitted_params) of your ClassDate admin panel has nothing to do with the permitted parameters within the ClassSection admin panel. Treat them as separate controllers within the app.
Adding the allow_destroy: true option to the has_many call will add a checkbox to the form to allow you to delete a class time upon form submission.

Resources