Last active
May 25, 2018 17:58
-
-
Save ansnisar/ac2f025ae2de13d7da2c to your computer and use it in GitHub Desktop.
Rails: Multi-level nested form (has_many association with checkboxes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bridge table between vendors & users | |
class AssignVendor < ActiveRecord::Base | |
belongs_to :vendor | |
belongs_to :user | |
has_many :assign_vendor_docs | |
has_many :vendor_files, through: :assign_vendor_docs | |
accepts_nested_attributes_for :assign_vendor_docs, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bridge table between vendor_files & assign_vendors(also a bridge table) | |
class AssignVendorDoc < ActiveRecord::Base | |
belongs_to :vendor_file | |
belongs_to :assign_vendor | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= simple_form_for [@new_user] do |f| %> | |
<%= f.input :first_name, input_html: {required: true} %> | |
<%= f.input :email, input_html: {required: true} %> | |
<% Vendor.all.each_with_index do |vendor, index| %> | |
<%= check_box_tag "user[assign_vendors_attributes][#{index}][vendor_id]", vendor.id, @user.vendors.include?(vendor), id: dom_id(vendor) %> | |
<%= label_tag dom_id(vendor), vendor.name %> | |
<% vendor.vendor_files.each_with_index do |file, f_index| %> | |
<%= check_box_tag "users[assign_vendors_attributes][#{index}][assign_vendor_docs_attributes][#{f_index}][vendor_file_id]", file.id, false, id: dom_id(file) %> | |
<%= label_tag dom_id(file), file.filename %> | |
<% end %> | |
<% end %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :assign_vendors | |
has_many :vendors, through: :assign_vendors | |
accepts_nested_attributes_for :assign_vendors, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserController < ApplicationController | |
def new | |
@new_user = User.new | |
@new_user.assign_vendors.build | |
respond_with(@new_user) | |
end | |
def create | |
@user = User.new(user_params) | |
if @user.save | |
flash[:notice] = 'User successfully created.' | |
end | |
redirect_to #anywhere you want | |
end | |
def user_params | |
params.require(:user).permit(:first_name, :last_name, :email, | |
assign_vendors_attributes: [:vendor_id, :_delete, assign_vendor_docs_attributes: [:vendor_file_id, :_delete]]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vendor < ActiveRecord::Base | |
has_many :vendor_files, dependent: :destroy | |
has_many :assign_vendors | |
has_many :users, through: :assign_vendors | |
accepts_nested_attributes_for :vendor_files, allow_destroy: true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VendorFile < ActiveRecord::Base | |
belongs_to :vendor | |
has_many :assign_vendor_docs | |
has_many :assign_vendors, through: :assign_vendor_docs | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment