I want to populate one selection field based on choice from other selected field. It's about categories / subcategories, when user is trying to submit new product. User picks main category in field 1, after picked main category he gets option to pick from field 2 children subcategories of picked main category from field 1. It's based on associations so it makes it harder for me.
Product model:
class Product < ActiveRecord::Base
belongs_to :user
has_many :categorizations
has_many :categories, through: :categorizations
end
Category model:
class Category < ActiveRecord::Base
belongs_to :parent, class_name: "Category"
has_many :children, class_name: "Category", foreign_key: "parent_id"
has_many :categorizations
has_many :products, through: :categorizations
end
Categorization model:
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
new.html.erb
<%= simple_form_for #product, :html => { multipart: true } do |f| %>
<%= f.association :categories, collection: Category.where(parent_id: nil), prompt: 'Choose category' %>
<%= f.association :categories, collection: Category.where.not(parent_id: nil), prompt: 'Choose subcategory' %>
<%= f.button :submit, "Submit product" %>
<% end %>
UPDATE:
class ProductsController < ApplicationController
def update_categories
#categories = Category.where("parent_id = ?", params[:category_id])
respond_to do |format|
format.js
end
end
end
some routes:
get 'products/update_categories', as: 'update_categories'
Related
I have three objects: Contact, Sector, and Contact_sector.
Contact contains an id and some other irrelevant (non-reference) columns
Sector contains an id and sector column with ~10 editable entries
Contact_sector has a contact_id reference and a sector_id reference. In my mind I imagine that every sector that applies to some contact can be found here, and if a sector is un-applied it is removed from here.
I want to have a collection of checkboxes in the contact _form formed from list of entries in :sectors, but updating the form with certain boxes ticked adds/removes entries from :contact_sectors.
Where am I going wrong?
UPDATED: Fixed strong_params to permit sectors, now I am unable to find the sectors by id ActiveRecord::RecordNotFound (Couldn't find Sector with ID=["1", ""] for Contact with ID=1)
Models:
class Contact < ActiveRecord::Base
has_many :contact_sectors
has_many :sectors, through: :contact_sectors
accepts_nested_attributes_for :contact_sectors, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :sectors, :reject_if => :all_blank, :allow_destroy => true
end
class Sector < ActiveRecord::Base
has_many :contact_sectors
has_many :contacts, through: :contact_sectors
def name_with_initial
"#{sector}"
end
end
class ContactSector < ActiveRecord::Base
belongs_to :contact
belongs_to :sector
end
View:
<%= f.fields_for(:sectors) do |s| %>
<%= s.collection_check_boxes :id, Sector.all,
:id, :name_with_initial,
{ prompt: true }, { class: 'form-control' } %>
<% end %>
Controller
def edit
#contact.sectors.build
end
def contact_params
#Not sure if I need something like this or not
params['contact']['sectors'] = params['contact']['sectors']['id'].split(',')
params.require(:contact).permit(:firstname, :lastname,
contact_sectors_attributes: [:id],
sectors_attributes: [:_destroy, {:id => []}])
end
Instead of creating the join model explicitly you can just declare the relationship as has_many through: and let ActiveRecord handle the join model:
class Contact < ActiveRecord::Base
has_many :contact_sectors
has_many :sectors, through: :contact_sectors
accepts_nested_attributes_for :sector,
reject_if: :all_blank, allow_destroy: true
end
class Sector < ActiveRecord::Base
has_many :contact_sectors
has_many :contacts, through: :contact_sectors
end
class ContactSector < ActiveRecord::Base
belongs_to :contact
belongs_to :sector
end
<%= form_for(#contact) do |f| %>
<%= f.fields_for(:sectors) do |s| %>
<%= s.collection_check_boxes :id, Sector.all,
:id, :name_with_initial,
{ prompt: true }, { class: 'form-control' } %>
<% end %>
<% end %>
models
class Contact < ActiveRecord::Base
has_many :sectors, through: :contact_sectors
has_many :contact_sectors
accepts_nested_attributes_for :sectors
end
class Sector < ActiveRecord::Base
has_many :contacts, :through => :contact_sectors
has_many :contact_sectors
end
class ContactSector < ActiveRecord::Base
belongs_to :contact
belongs_to :sector
end
view
<%= form_for(#contact) do |f| %>
<% Sector.all.each do |sector| %>
<%= check_box_tag "contact[sector_ids][]", sector.id, f.object.sectors.include?(sector) %>
<%= sector.sector %>
<% end %>
<% end %>
controller
def update
#To make sure it updates when no boxes are ticked
#contact.attributes = {'sector_ids' => []}.merge(params[:contact] || {})
end
def contact_params
params.require(:contact).permit(:firstname, :lastname, sector_ids: [])
end
Recommended reading:
http://millarian.com/rails/quick-tip-has_many-through-checkboxes/
Rails 4 Form: has_many through: checkboxes
I have the scenario where an author has and belongs to many books, vice versa. Following the instructions for setting up associations in a one-to-many relationship works fine but when a many-to-many relationship introduced I get this error message whenever I try to create or update my book model.
undefined method `author' for #<Book:0x007fb91ae56a70>
As far as setting up how authors are chosen for a book I'm using the code provided by the token-input railscast here with a few alterations.
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, through: :authorships
def self.tokens(query)
authors = where("name like ?", "%#{query}%")
if authors.empty?
[{id: "<<<#{query}>>>", name: "Add New Author: \"#{query}\""}]
else
authors
end
end
def self.ids_from_tokens(tokens)
tokens.gsub!(/<<<(.+?)>>>/) {create!(name: $1).id}
tokens.split(',')
end
end
class Book < ActiveRecord::Base
attr_reader :author_tokens
include PublicActivity::Model
tracked owner: :author
has_many :authorships
has_many :authors, through: :authorships
def author_tokens=(ids)
self.author_ids = Author.ids_from_tokens(ids)
end
end
Form View
<%= form_for(#book) do |f| %>
...
<div class="field">
<%= f.text_field :author_tokens, label: 'Author', input_html: {"data-pre" => #book.authors.to_json} %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
There is no author relationship in your Book model.
What
tracked owner: :author
does is basically calling method author on your Book instance. You should try :authors
But!
That won't solve your problem because owner can only be one. So you can do something like:
tracked owner: proc {|_, book| book.authors.first }
to set the owner to the first author the book has.
class Author < ActiveRecord::Base
has_many :author_books, inverse_of: :author, dependent: :destroy
accepts_nested_attributes_for :author_books
has_many :books, through: :author_books
end
class Book < ActiveRecord::Base
has_many :author_books, inverse_of: :book, dependent: :destroy
accepts_nested_attributes_for :author_books
has_many :authors, through: :author_books
end
class AuthorBook < ActiveRecord::Base
validates_presence_of :book, :author
end
============= view ==============
<%= form_for #book do |f| %>
<%= f.text_field :title %>
<%= f.fields_for :author_books do |f2| %>
<%# will look through all author_books in the form builder.. %>
<%= f2.fields_for :author do |f3| %>
<%= f3.text_field :name %>
<% end %>
<% end %>
<% end %>
I am trying to sort comments into events using a has_many :through association using checkboxes however the selected events are not saved. Here are my models:
class Comment < ActiveRecord::Base
has_many :categorizations
has_many :events, :through => :categorizations
end
class Event < ActiveRecord::Base
has_many :categorizations
has_many :comments, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :comment
belongs_to :event
end
My comments form looks like this:
<%= simple_form_for [#post, #comment] do |f| %>
<%= f.input :title %>
<%= f.association :events, :as => :check_boxes %>
<%= f.submit "Save" %>
After reading this answer, I added this to my event and comment controllers with no luck:
def comment_params
params.require(:comment).permit(:post_id, :title, :categorization_ids => [])
end
Try:
def comment_params
params.require(:comment).permit(:post_id, :title, :event_ids => [])
end
It's hard to know what's going on though without recreating it or seeing server logs, Hopefully this will solve it.
I'm quite new to rails, so my question may seem noobish.
I have HABTM self association. A product can become a "COMBO" of products, having N products.
class Product < ActiveRecord::Base
has_many :combo_elements, class_name: "ComboElement", foreign_key: :combo_id
has_many :element_combos, class_name: "ComboElement", foreign_key: :element_id
has_many :combos, :through => :element_combos
has_many :elements, :through => :combo_elements
accepts_nested_attributes_for :elements
end
class ComboElement < ActiveRecord::Base
belongs_to :combo, :class_name => 'Product'
belongs_to :element, :class_name => 'Product'
end
With the above code, I can list all "elements" of a combo. Also I can list all "combos" a product is part of (the ComboElement table has a combo_id and an element_id)
Here is my form
<%= f.simple_fields_for :elements, #element do |b| %>
<%= b.input :element_id, :collection => Product.all.collect{ |t| [t.name, t.id ]}, selected: b.object.id %>
<%= b.link_to_remove "Remove this element" %>
<% end %>
I can successfully list all Products a combo has, but whenever I try to update, it simply doesn't work.
My Product controller has the following code
def product_params
params[:product].permit(:name, :price, elements_attributes: [:id, :element_id, :_destroy])
end
I appreciate any help.
Thanks in advance!
My Category model :
class Category < ActiveRecord::Base
has_many :item_categoryships
has_many :items, :through => :item_categoryships
end
My Item model :
class Item < ActiveRecord::Base
has_many :item_categoryships
has_many :categories, class_name: 'ItemCategoryship', foreign_key: 'category_id', :through => :item_categoryships
end
My ItemCategoryship model:
class ItemCategoryship < ActiveRecord::Base
belongs_to :item
belongs_to :category
end
And in views/items/edit.html.erb, I wrote simple form code like this:
<%= simple_form_for(#item) do |f| %>
<%= f.association :categories, collection: #categories, as: :check_boxes %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
The #categories above, I wrote this in controller:
#categories = current_user.categories
But I hit a problem, they can't save to database!!
I couldn't find out what the problem was. Please help me....
Thanks you all.
For the view, this SO answer should help:
https://stackoverflow.com/a/9917006/2463468
You may first need to make sure your associations make sense. Perhaps you only need a has_many on Items and belongs_to on Category.
You might be able to do without ItemCategoryship.