ActiveAdmin ::Show 4 textbox in active admin using has_many relationship - ruby-on-rails

I have one poll table having with question filed and each question have 4 answer which is has_many relation ship with answer table
class Poll < ActiveRecord::Base
has_many :answer
end
My answer model
class Answer < ActiveRecord::Base
belongs_to :poll, :class_name => "Poll", :foreign_key => "question_id"
end
My active admin form is
form :html => {:validate => true} do |f|
f.inputs "Polls" do
f.input :question
f.has_many :answer, :sortable => :priority do |ff|
ff.input :question[]
end
f.input :status,as: 'hidden',:input_html => { :value => f.object.status.present? ? f.object.status : 'Unpublished' }
f.input :position,as: 'hidden',:input_html => { :value => Poll.count().to_i + 1}
end
f.actions
end
I want to show only 4 answer textbox into my form, how can i do that

Add the following to your Polls Controller-
def new
#poll = Poll.new
4.times do
#poll.answers.build
end
end

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

ActiveAdmin has_many relation record doesnt save

Im trying to save a post with category relation.
These are my models
class Post < ActiveRecord::Base
has_many :categorizes
has_many :post_categories, :through=>:categorizes
accepts_nested_attributes_for :post_categories
end
class PostCategory < ActiveRecord::Base
has_many :categorizes
has_many :posts, :through=>:categorizes
end
class Categorize < ActiveRecord::Base
belongs_to :post
belongs_to :post_category
end
and in ActiveAdmin post.rb.
ActiveAdmin.register Post do
permit_params :title, :content, post_category_ids:[:id]
index do
selectable_column
id_column
column :title
column :post_category_id
column :created_at
actions
end
filter :created_at
form do |f|
f.inputs "Post Details" do
f.input :title
f.input :content,:input_html => { :class => "tinymce_editor" }
#f.input :post_categories, :as=> :check_boxes#, :collection => PostCategory.all
end
f.has_many :post_categories ,new_record: false do |c|
c.inputs do
c.input :title
end
end
f.actions
end
controller do
defaults :finder => :find_by_slug_url
end
end
I need to see my all categories from post_categories and i should select more than one.
i check in rails console but the post hasnt any category.
Post.First.post_categories equal to []
Try post_category_attributes instead of post_category_ids
See more here.

rails how get related id on view page

i'm trying to make this conditional:
<% if current_boutique.boutique_kind(3) %>
Brand: <span><%= current_boutique.name %></span>
<% else %>
<%= p.input :brand_id, :as => :select, :collection=> Brand.find(:all, :order=>:name).collect{ |b| [b.name,b.id, b.name]},
:label => "Marca", :prompt => 'Select Brand', :required => true %>
<% end %>
but the boutique_kind(3) show all boutiques! how i just get the boutiques with boutiques_kind id 3 method!
boutiqueKind Controller
def show
#boutique_kind = BoutiqueKind.find(params[:id])
end
boutiqueKind model
attr_accessible :kind, :slug
has_many :boutiques
has_many :products, :through => :boutiques
#belongs_to :gender
accepts_nested_attributes_for :boutiques
attr_accessible :boutiques, :boutiques_attributes, :kind
Boutique Controller
class Boutique < ActiveRecord::Base
belongs_to :user
belongs_to :boutique_kind
end
You should be able to find the boutique by ID with:
BoutiqueKind.find(3)
If I'm misunderstanding, and id is not a unique id (in which case I suggest you rename the column to something other than id), you can get all records where a certain field == a specific value with:
BoutiqueKind.where(id: 3)

Activeadmin formtastic dynamic select

I would like to make a dynamic select option via Activeadmin's formtastic like so:
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :as => :select, :collection => User.where(:admin => 'false')
#selects user from list. WORKING
f.input :student_id, :as => :select, :collection => Student.joins(lessons: :user)
#collection of students will change to students who have lessons with chosen user. NOT WORKING, returns all students who have lessons.
f.input :lesson_id, :as => :select, :collection => Lesson.joins(:student, :user)
#collection of lessons will change to reflect lessons connected by chosen user and student. NOT WORKING, returns all lessons.
end
f.buttons
end
My amateurish code is obviously not working as I intended it to. What changes should I make?
I have 4 models as below:
class Student < ActiveRecord::Base
has_many :lessons
has_many :users, through: :lessons
has_many :exam_registrations, through: :lessons
class Lesson < ActiveRecord::Base
belongs_to :user
belongs_to :student
belongs_to :exam_registration
class User < ActiveRecord::Base
has_many :lessons
has_many :students, through: :lessons
has_many :exam_registrations, through: :lessons
class ExamRegistration < ActiveRecord::Base
has_many :lessons
has_many :users, through: :lessons
has_many :students, through: :lessons
SOLVED
For anyone else wrestling with the same problem, look at this railscast
here's how I implemented multiple dynamic select menus in activeadmin:
config/initializers/active_admin.rb
config.register_javascript 'exam_registrations.js.coffee'
app/admin/exam_registrations.rb
form do |f|
f.inputs "Exam Registration Details" do
f.input :user_id, :label => 'Teacher', :as => :select, :collection => User.where(:admin => 'false', :active => true).order(:name), :include_blank => true
f.input :student_id, :hint => 'Students grouped by teacher names', :as => :select, :collection => option_groups_from_collection_for_select(User.where(:admin => false, :active => true).order(:name), :students, :name, :id, :name)
f.input :lesson_id, :hint => 'Lessons grouped by student names', :as => :select, :collection => option_groups_from_collection_for_select(Student.where(:active => true).order(:name), :lessons, :name, :id, :name)
end
f.buttons
end
app/assets/javascripts/exam_registrations.js.coffee
#first menu
jQuery ->
$('#exam_registration_student_id').parent().hide()
students = $('#exam_registration_student_id').html()
$('#exam_registration_user_id').change ->
user = $('#exam_registration_user_id :selected').text()
escaped_user = user.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(students).filter("optgroup[label='#{escaped_user}']").html()
if options
$('#exam_registration_student_id').html(options)
$('#exam_registration_student_id').parent().show()
else
$('#exam_registration_student_id').empty()
$('#exam_registration_lesson_id').empty()
# second menu
$('#exam_registration_lesson_id').parent().hide()
lessons = $('#exam_registration_lesson_id').html()
$('#exam_registration_student_id').click ->
student = $('#exam_registration_student_id :selected').text()
escaped_student = student.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/#])/g, '\\$1')
options = $(lessons).filter("optgroup[label='#{escaped_student}']").html()
if options
$('#exam_registration_lesson_id').html(options)
$('#exam_registration_lesson_id').parent().show()
else
$('#exam_registration_lesson_id').empty()
restart the server and the menus work!

Rails simple_form association with nested form

My application has 3 models : consultant, project and appointment
I am using a nested form with simple_form gem
class Consultant < ActiveRecord::Base
has_many :appointments
end
class Project < ActiveRecord::Base
has_many :appointments, :dependent => :destroy
accepts_nested_attributes_for :appointments, :allow_destroy => true
end
class Appointment < ActiveRecord::Base
belongs_to :consultant
belongs_to :project
end
My form is as follows :
= simple_nested_form_for(#project) do |f|
%div.field
= f.input :name, :label => 'Nom du projet'
= f.simple_fields_for :appointments do |builder|
= render 'appointment_fields', :f => builder
= f.link_to_add "ajouter un consultant", :appointments
%div
%div.actions
= f.submit
with the partial :
%p.fields
= f.input :consultant_id, :input_html => { :class => 'special' }
= f.association :consultant
= f.input :nb_days, :input_html => { :class => 'special',:size => 10 }
= f.input :rate, :input_html => {:size => 10}
= f.link_to_remove "Remove this task"
Is it possible to do something as simple as this with simple_form ?
The answer is YES : it works nicely
The errors is because there is no association called consultant on the model Appointment. Use consultants instead.

Resources