Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Last active March 18, 2020 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save staycreativedesign/d508e1b5b0ecd3367e0d9d7c364de358 to your computer and use it in GitHub Desktop.
Save staycreativedesign/d508e1b5b0ecd3367e0d9d7c364de358 to your computer and use it in GitHub Desktop.
%h1
hello
- detail_subs.each do |sub|
.form-group
= sub.title
= binding.pry
= select_tag(:details, options_for_select(sub.details.pluck('title', 'id'), entry.detail_ids), multiple: true, class: 'chosen-select form-control')
:javascript
$(function() {
$(".chosen-select").chosen();
})
.form-group
= f.label :detail_category_ids, 'Select Amenities'
= f.select(:detail_category_ids, options_from_collection_for_select(DetailCategory.all, 'id', 'title', f.object.detail_categories), {}, class: 'chosen-select-detail form-control')
.form-group
#detail_subs
:javascript
$(".chosen-select-detail").chosen().change(function(e){
var detail_category_id = e.target.value
$.get(
'/admin/entries/#{ @entry.slug }/' + detail_category_id + '/detail_subs',
{},
function(data){
console.log(data)
}, 'script')
});
class Detail < ApplicationRecord
belongs_to :detail_sub
has_many :entry_details
has_many :entries, through: :entry_details
end
class DetailCategory < ApplicationRecord
has_many :entry_detail_categories
has_many :entries, through: :entry_detail_categories
has_many :detail_subs
end
class DetailSub < ApplicationRecord
belongs_to :detail_category
has_many :details
end
class Admin::DetailSubsController < Admin::PagesController
def index
@entry = Entry.friendly.find(params[:entry_id])
detail_category = DetailCategory.find params[:detail_category_id]
@detail_subs = detail_category.detail_subs
respond_to do |format|
format.js { render layout: false }
end
end
end
def entry_params
params.require(:entry).permit(:title, :subtitle, :of_type, :intro, :content,
:promo, :author, :address, :city, :state, :zipcode,
:lat, :long, :directions, :website, :email, :synonyms,
:facebook, :instagram, :twitter, :pinterest, :details,
:meta_description, :meta_title, :meta_keywords,
:facility, :group, :internet, :room, :room_amenities,
:discounts, :slug, :type_id, :lake_assn,
:featured_image, :is_featured, images: [],
phone_numbers_attributes: [:id, :label, :number, :_destroy],
videos_attributes: [:id, :url, :_destroy],
hours_attributes: [:id, :title, :label_1, :label_2, :_destroy],
prices_attributes: [:id, :label, :price, :_destroy],
entry_detail_ids: [], related_entry_ids: [],
detail_category_ids: [], topic_ids: [],
documents_attributes: [:id, :position, :title],
)
end
class Entry < ApplicationRecord
...
has_many :entry_details
has_many :details, through: :entry_details
has_many :entry_detail_categories
has_many :detail_categories, through: :entry_detail_categories
...
end
class EntryDetail < ApplicationRecord
belongs_to :entry
belongs_to :detail
end
class EntryDetailCategory < ApplicationRecord
belongs_to :entry
belongs_to :detail_category
end
:plain
$('#detail_subs').html("#{j render(partial: 'admin/entries/detail_subs', locals: {detail_subs: @detail_subs, entry: @entry})}")
@staycreativedesign
Copy link
Author

When I submit I check for

EntryDetailCategory(id: integer, entry_id: integer, detail_category_id: integer, created_at: datetime, updated_at: datetime)

[9] pry(#<#Class:0x00007ffc8848dbb8>)> EntryDetailCategory.all
CACHE EntryDetailCategory Load (0.0ms) SELECT "entry_detail_categories".* FROM "entry_detail_categories"
↳ app/views/admin/entries/_form.haml:76
=> []
[10] pry(#<#Class:0x00007ffc8848dbb8>)>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment