nested collection_set - ruby-on-rails

I'm building a recurring billing system and am having trouble with a nested form. The following code works, but makes a POST with:
"customer"=>{"service"=>{"service_id"=>"1"}}
It should be Customer.services instead of Customer.service. However, if I change the form to reference fields_for :services, it doesn't render a dropdown at all.
_form.html.haml
= form_for #customer do |f|
= f.fields_for :service do |service_fields|
= service_fields.collection_select(:service_id, Service.all, :id, :name, { :prompt => 'Select Package' })
= f.submit "Add Service", class: "btn"
models/customer.rb
class Customer < ActiveRecord::Base
has_many :subscriptions, :dependent => :destroy
has_many :services, :through => :subscriptions
accepts_nested_attributes_for :services
end
models/service.rb
class Service < ActiveRecord::Base
has_many :customers, :through => :subscriptions
has_many :subscriptions
end
models/subscription.rb
class Subscription < ActiveRecord::Base
belongs_to :customer
belongs_to :service
end

So, in this situation you want to build a subscription on the customer. Since you're pretty much on the right track, it should be as simple as changing this
_form.html.haml
= form_for #customer do |f|
= f.fields_for :subscriptions do |subscription_fields|
= subscription_fields.collection_select(:service_id, Service.all, :id, :name, { :prompt => 'Select Package' })
= f.submit "Add Service", class: "btn"
models/customer.rb
class Customer < ActiveRecord::Base
has_many :subscriptions, :dependent => :destroy
has_many :services, :through => :subscriptions
accepts_nested_attributes_for :subscriptions
end
Right now the reason why you're getting anything rendered is because you're causing the form to submit with a new "attribute" called service with then returns the data from the fields_for. With accepts_nested_attributes_for {model} you should be looking for a params with something like {model}_attributes
Also the reason why you're not getting anything rendered when you use :services in the fields_for is because Service doesn't respond to service_id.

I was able to figure it out finally (with the help of a colleague.) I made all of the above changes thanks to #Azolo's reply above with also changing the form view to:
_form.html.haml
= f.fields_for :subscriptions, #customer.subscriptions.build do |builder|

Related

Form helpers for nested attributes has_many through in rails 4

I have 3 models with has_many through association:
class Spot < ActiveRecord::Base
has_many :seasons
has_many :sports, through: :seasons
accepts_nested_attributes_for :sports, :seasons
end
class Sport < ActiveRecord::Base
has_many :seasons
has_many :spots, through: :seasons
end
class Season < ActiveRecord::Base
belongs_to :spot
belongs_to :sport
end
I want to create a form so that editing the Spot you can create/edit Season for particular Sport.
I've got it working this way:
= form_for([#country, #spot], :html => { :multipart => true }) do |f|
#some Rails code here
= f.fields_for :seasons do |builder|
= builder.select :sport_id, options_from_collection_for_select(Sport.all, :id, :name, :sport_id)
= builder.text_field :months
= builder.hidden_field :spot_id
However it doesn't mark any Sport as "selected". I don't know what to substiture ":sport_id" for in the options.
I've got it working properly without Rails form helpers, but it seems that could be done with helpers:
- #spot.seasons.each do |season|
= select_tag "spot[seasons_attributes][][sport_id]", options_from_collection_for_select(Sport.all, :id, :name, season.sport.id)
= hidden_field_tag 'spot[seasons_attributes][][id]', season.id
= text_field_tag 'spot[seasons_attributes][][months]', season.months
Thanks in advance,
Vadim
Use collection_select helper on a form builder.
builder.collection_select(:sport_id, Sport.all, :id, :name)

Has_many through checkboxes (simple_form) not saving

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.

Nested Form and database update

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!

How to create checkbox in forms for many to many association

I am having a many to many association and totally confused on how to create checkboxes.
model: lodge.rb
has_many :lodge_facilities, :dependent => :destroy
has_many :facilities, through: :lodge_facilities, :dependent => :destroy
accepts_nested_attributes_for :facilities
model: lodge_facility.rb
belongs_to :lodge
belongs_to :facility
model: facility.rb
has_many :lodge_facilities, :dependent => :destroy
has_many :facilities, through: :lodge_facilities, :dependent => :destroy
in my form i tried
<%= form_for #lodge, :class =>'lodge_form', url: admins_lodge_path, method: :put,:html => {:multipart => true} do |f|%>
<%= f.fields_for :lodge_facilities do |fac| %>
<%= fac.check_box :ac %>
<%= fac.label :ac,'AC' %>
<%= fac.check_box :wifi %>
<%= fac.label :ac,'Wifi' %>
<% end %>
this displays checkbox but when i submit the form in my console i see Unpermitted parameters: lodge_facilities
In my controller i have added
def lodge_params
params.require(:lodge).permit(lodge_facilities_attributes:[:id,:lodge_id,:lodge_facility_id],facilities_attributes: [:id,:ac,:wifi,:internet,:bar,:restaurant,:gym,:pool,:laundry,:parking,:transportation] )
end
What am i missing?
Try this one:
def lodge_params
params.require(:lodge).permit(lodge_facilities_attributes:[:id,:lodge_id,:lodge_facility_id],lodge_facilities: [:id,:ac,:wifi,:internet,:bar,:restaurant,:gym,:pool,:laundry,:parking,:transportation] )
end
also you can use nested_form gem
https://github.com/ryanb/nested_form

Rails 3 - Using tags to associate photos with polymorphic Taggable class

My goal is to create a system for associating photos with objects from any of several classes (Events, Organizations, Developments) using Tags. For the life of me, I can't get this to work out, despite the fact that it seems like a pretty common situation.
I'm relatively new to anything but the most basic Rails development, so I'm having a hard time forming the question. Please excuse any misnomers.
Tag model:
class Tag < ActiveRecord::Base
attr_accessible :photo_id, :taggable_id, :taggable_type
belongs_to :photo
belongs_to :taggable, :polymorphic => true
end
Photo model:
class Photo < ActiveRecord::Base
attr_accessible :tags_attributes
has_many :tags, :dependent => :destroy
accepts_nested_attributes_for :tags, :reject_if => lambda { |a| a[:taggable_id].blank? }
end
Event model:
class Event < ActiveRecord::Base
has_many :tags, :as => :taggable, :dependent => :destroy
end
Organization model:
class Organization < ActiveRecord::Base
has_many :tags, :as => :taggable, :dependent => :destroy
end
Development model:
class Development < ActiveRecord::Base
has_many :tags, :as => :taggable, :dependent => :destroy
end
In my Photo fields, I'm trying to use the nested_form gem to add tags to the photo (so I can later call those tagged objects in the photo's views, and the photo in the tagged object's views).
photos/new.html.erb (I have included the nested_form javascript)
<% nested_form_for #photo, :html => { :multipart => true } do |f| %>
...
<%= f.fields_for :tags do |tag_form| %>
<%= tag_form.collection_select :taggable_id, Taggable.all, :id, :name %>
<%= tag_form.link_to_remove "remove" %>
<% end %>
<p><%= f.link_to_add "Add tag", :tags %></p>
...
<% f.submit "Add photo" %>
<% end %>
Is the structure of my models suitable for what I'm trying to do?
and, if so,
How can I properly specify both he :taggable_id and :taggable_type in my nested form?
Thanks in advance for any guidance!
You need to make your Photo model polymorphic and use something like attachment_fu / paperclip to upload photos.
class Photo < ActiveRecord::Base
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 2000.kilobytes,
:resize_to => '500x500>',
:thumbnails => { :thumb => '215x215>'}
validates_as_attachment
belongs_to :attachable, :polymorphic => true
end
class Event < ActiveRecord::Base
has_many :tags, :as => :taggable, :dependent => :destroy
has_many :attachments, :as => :attachable
end
class Organization < ActiveRecord::Base
has_many :tags, :as => :taggable, :dependent => :destroy
has_many :attachments, :as => :attachable
end
class Development < ActiveRecord::Base
has_many :tags, :as => :taggable, :dependent => :destroy
has_many :attachments, :as => :attachable
end
Your photos table should have attachable_id and attachable_type columns. With this you should be able to add photos to different object all stored in polymorphic photo model. Tagging also uses polymorphic associations and works in similar way. the documentation for the plugin would help. Hope that works for you.

Resources