Creating a record with Associations - Active Admin - ruby-on-rails

I am using active admin and seem to be struggling with creating a record with an association. I have set this up in the normal way
class Membership < ActiveRecord::Base
belongs_to :member
attr_accessible :membership_type
end
class Member < ActiveRecord::Base
has_one :membership
accepts_nested_attributes_for :membership
attr_accessible :membership_attributes, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end
I then want to create a New member along with their membership type in the same form, so my member.rb looks like this so far
ActiveAdmin.register Member do
# Set Which Columns are to be displayed on the index page
index do
column :forename
column :middlename
column :surname
column :house_no
column :house_name
column :street
column :town
column :postcode
column :home_tel
column :mobile_tel
column :work_tel
column :email
column :membership do |member|
member.membership.map{ |ms| ms.membership_type}
end
default_actions
end
# Set Which Columns are to be displayed on Create New Member
form do |f|
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
f.input :house_no
f.input :house_name
f.input :street
f.input :town
f.input :postcode
f.input :home_tel
f.input :mobile_tel
f.input :work_tel
f.input :email
end
f.inputs :for => [:membership, f.object.membership || Membership.new] do |m|
m.input :membership_type, :label => 'Membership Type', :as => :select, :collection => Membership.all.map{|m| [m.membership_type]}
end
f.actions
end
end
This displays a select box where i can choose from various Membership types, but i dont seem to be passing the correct value when creating the record,
This is an example of what is being posted
member[forename]:Name 1
member[middlename]:Name 2
member[surname]:Name 3
member[house_no]:1
member[house_name]:
member[street]:Test Street
member[town]:Test Town
member[postcode]:CF23 7BD
member[home_tel]:01633222222
member[mobile_tel]:07864582356
member[work_tel]:01633555555
member[email]:test#mail.com
membership_attributes"=>{"membership_type"=>"Student"}
commit:Create Member
This doesn't seem right does it? Also when i try and view the record
column :memberships do |member|
member.memberships.map{ |ms| ms.membership_type}
end
I get the following mysql error
Mysql2::Error: Unknown column 'memberships.member_id' in 'where clause': SELECT `memberships`.* FROM `memberships` WHERE `memberships`.`member_id` = 10
Could anyone point me in the right direction please or can anyone see where im going wrong?
Much appreciated

Instead of f.input :memberships try this:
f.has_many :memberships do |pf|
pf.input :membership_type
end
and close your member object fields like this
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
...
end
So your form should look like this:
form do |f|
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
# .. the rest of your fields
end
f.has_many :memberships do |pf|
pf.input :membership_type
end
f.actions
end

Regarding the MySQL error, i think for the association to work you need to add the column member_id to the memberships table. You can create the migration with
rails g migration add_member_id_to_memberships member_id:integer
Then migrate with
bundle exec rake db:migrate

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

Ruby on Rails and ActiveAdmin: add all object all at once instead add one by one

I'm using Ruby on Rails 5 API app with Active Admin.
I have a model called Subscription with has_many courses (Course is also a model).
When I add courses to a subscription, I need to do it one by one, like in this picture:
What I want is to have an option to add all the courses to a subscription all at once, instead of one by one. I tried using checkbox for multiple selection but it didn't worked. This is the code:
ActiveAdmin.register Subscription do
# require 'lib/app_languages.rb'
permit_params :name, :seat_limit, :domain, :language, :organization, course_ids: [], user_ids: [], subscription_courses_attributes: [:id, :course_id, :_destroy, :_create, :_update]
config.sort_order = 'id_asc'
# Index
index do
id_column
column :name
column :seat_limit
column :domain
column :organization
column :language do |subscription|
AppLanguages.languages[subscription.language]
end
column "Unlocked courses", :courses
column :created_at
actions
end
# Show
show do
attributes_table do
row :name
row :language do |subscription|
AppLanguages.languages[subscription.language]
end
row :seat_limit
row :domain
row :organization
row :created_at
end
panel 'Unlocked Courses' do
table_for subscription.courses do
column :id
column "Title" do |course|
link_to course.title, admin_course_path(course)
end
column "language" do |course|
AppLanguages.languages[course.language]
end
column :author
end
end
panel 'Subscribed users' do
table_for subscription.users do
column :id
column "name" do |user|
link_to user.name, admin_user_path(user)
end
column :email
column "language preference" do |user|
AppLanguages.languages[user.language]
end
end
end
end
# Edit
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs 'Details' do
f.input :name
f.input :language, :as => :select, :collection => AppLanguages.languages_array
f.input :seat_limit
f.input :domain
f.input :organization
end
f.has_many :subscription_courses do |sub_c|
sub_c.inputs "Unlocked Courses" do
if !sub_c.object.nil?
sub_c.input :_destroy, as: :boolean, label: "Destroy?"
end
sub_c.input :course ### should add here option for add all
end
end
f.inputs 'Subscribed users:' do
f.input :users
end
f.actions
end
end
Using for the array the as: :select form input with multi-select should help. Something like this:
sub_c.input :items, as: :select,
collection: courses_collection,
multiple: true
You can try using check_boxes
sub_c.input :items, as: :check_boxes, collection: courses_collection

NoMethodError in Admin::Restaurants#new Active Admin Rails 5

I added two email fields to my Restaurant table:
class AddEmailsToRestaurants < ActiveRecord::Migration[5.1]
def change
add_column :restaurants, :email2, :string, null: true, default: nil
add_column :restaurants, :email3, :string, null: true, default: nil
end
end
I ran rails db:migrate and I want to add these two fields in the Active Admin form in order to create new Restaurants from the admin interface:
ActiveAdmin.register Restaurant do
permit_params :email, :email2, :email3, [...]
filter :name
form do |f|
f.inputs "Restaurant" do
f.input :name
f.input :email
f.input :email2
f.input :email3
f.input :password
f.input :password_confirmation
f.input :address
f.input :address2
f.input :phone_number
f.input :city
f.input :post_code
f.input :latitude
f.input :longitude
f.input :photo
end
f.actions
end
[...]
end
But then when I click on 'Create new Restaurant' in my Admin interface, I get the following error: NoMethodError in Admin::Restaurants#new undefined method 'email2' for #<Restaurant:0x007f9490cc0340>
How can I solve this ?
Note that a running development environment will not detect changes to a table structure after a migration.
If you add columns via a migration, be sure to stop and restart your development server.

Active Admin remove certain fields while going for updation

Hi i have an admin page using active admin gem.
thing is while creating a new page i should be able to input name, amount and interval..But while updating only name field must show..other 2 values shouldnt be updated. This is my active admin file. How to make this happen. Thanks in advance
ActiveAdmin.register SubscriptionPlan do
menu priority: 10
permit_params :name, :amount, :interval
index do
selectable_column
default_actions
column :name
column :amount
column :interval
end
form do |f|
f.inputs "Subscription Plan" do
f.input :name
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
f.actions
end
end
Try this
form do |f|
f.inputs "Subscription Plan" do
f.input :name
if f.object.new_record?
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
end
f.actions
end

ruby error with form builder (collection), Group expected, got String

Im new to ruby and got a problem
= simple_form_for #user do |f|
.form-inputs
= f.input :name
= f.input :email
= f.input :group_id, collection: Group.all.collect {|c| [c.name, c.id]}
.form-actions
= f.button :submit
The line that gives me an error message is this:
= f.input :group, collection: Group.all.collect {|c| [c.name, c.id]}
Same thing with
= f.input :group, collection: #groups
The User:
class User < ActiveRecord::Base
attr_accessible :email, :name, :group
belongs_to :group
The Group:
class Group < ActiveRecord::Base
attr_accessible :description, :name
has_many :users
I was also using the gem "immigration", which created the foreign key and this migration:
class AddKeys < ActiveRecord::Migration
def change
add_foreign_key "users", "groups", :name => "users_group_id_fk"
end
end
I saw the warning here about collections and form builders.
When I use
= f.input :group_id, collection: #groups
it prompts me an error message even before loading the page (not after submitting the form)
undefined method `group_id'
any help?
According to the Simple Form documentation, you should use f.input :group, and not f.input :group_id. The beauty is that you don't have to pass in the collection, it does it automagically:
f. input :group
This should render a select input with your groups listed.
Change your model from Group to XGroup or anything other than Group, I think you're conflicting with ActiveRecord's Group

Resources