Simple form remove associated record - ruby-on-rails

I'm using simple_form and I would give user ability to quickly remove an associated record. (eg. "checking/uncheking")
How is it possibile with simple_form? Is there another gem to help with this?
Parent has many children
<%= simple_form_for #parent do |f| %>
<%= f.simple_fields_for :childens do |p| %>
<%= p.input :title, as: :boolean %>
<% end %>
<% end %>
Rails 5.2

You don't need another gem for that. There are several things you need to do:
Add allow_destroy: true to the accepts_nested_attributes_for :children in the parent model
Add a <%= p.input :_destroy, as: :boolean %> to the nested form
Whitelist the _destroy pseudo attribute in your controller by listing it in children_attributes in the permit call
Essentially this is a feature of Rails' accepts_nested_attributes_for - it sets up the children_attributes setter to not only create/update associated records but also delete them in the presence of _destroy in the passed hash.

Related

JSONB: store_model gem. Modify content on posting data

I'm using StoreModel gem to wrap my JSON-backed DB column setting with an ActiveModel-like classes. This is my model simplified model
class Entity < ApplicationRecord
attribute :settings, Setting.to_type # settings is a jsonb datatype in the database
end
class Setting
include StoreModel::Model
attribute :setting1, :boolean
attribute :setting2, :boolean
attribute :setting3, :boolean
end
For the form I have the following
<%= form_for #entity do |f| %>
<%= fields_for :settings, #entity.settings do |ff| %>
<%= ff.check_box :setting1 %>
<%= ff.check_box :setting2 %>
<% end %
<% end %>
For an existing record, this would overwrite all values in the settings attribute of my Entity model, hence it would set setting3 no null (which is not passed by the form / params)! How can I submit values to keep existing values and just modify ones I submit.
It was discussed in the GitHub issue earlier, TLDR it's not possible out–of–the–box, but there is a workaround.

Rails 4 form to set has_many through additional column

I have a has_many association between Items and their Components through a table called ComponentItems. ComponentItems contains a column quantity in addition to item_id and component_id. How is it possible to add a number_field to my form that shows the quantity of each component required for an item? The form must contain a number_field for each Item in the database, even if no relationship exists (i.e. #item.component_ids.empty? == true).
class Item < ActiveRecord::Base
has_many :components, through: :component_items
has_many :component_items
end
class Component < Item
has_many :items, through: :component_items
has_many :component_items
end
class ComponentItem < ActiveRecord::Base
belongs_to :item
belongs_to :component
end
I believe I've tried every permutation of model, controller and form_builder possible, except the correct one.
In response to the answer below, here's a form that shows a checkbox and the item code for component items that make up one particular item;
<%= form_for [#item] do |f| %>
<%= f.collection_check_boxes :component_items, Item.active.where.not(sku: #item.sku).sort_by{|n| n.sku}, :id, :sku do |b| %>
<%= b.check_box %> <%= b.label %><br/>
<% end %>
<% end %>
So, ideally I'd replace the check_box with a number_field for quantity. How?
So it seems what I wanted is not so straightforward after all. In the end I opted for using some jQuery for adding extra Components to Items via a separate form. Trying to add/remove components and adjust the quantities was beyond me, so choosing to use separate forms for each user action seemed simpler. It may not be the most user-friendly way of working but it's the best I have.
To edit the quantities I did the following;
<% #item.component_items.each do |x| %>
<%= hidden_field_tag "item[component_items_attributes][][id]", x.id%>
<%= label_tag x.component.sku, x.component.sku.upcase, :class=>"col-md-3 control-label" %>
<%= number_field_tag "item[component_items_attributes][][quantity]", x.quantity, :class=>"col-md-5"%>
<%end %>
and ensured the Item model accepted nested attributes for component_items. Finally, add the nested params array for multiple component_items to items_controller.rb...
def item_params
params.require(:item).permit(
:component_items_attributes =>[:component_id, :item_id, :quantity, :id]
)
end
Note I didn't use fields_for which seemed to generate an extra component_items_attributes array that didn't make any sense at all.
This should work:
#item.components.to_a.sum(&:quantity)
This will throw an error if quantity on some component is nil, so you may try like this to avoid errors:
#item.components.to_a.map(&:quantity).compact.sum
UPDATE
<% #item.component_items.each do |component_item| %>
<%= form_for(component_item) do |f| %>
<div class="field">
<%= f.label :quantity, 'Quantity' %><br />
<%= f.number_field :quantity %>
</div>
<% end %>
<% end %>

Rails 4 - simple_form and pre-populating fields from url

I'm using simple_form and I'd like to pre-populate several fields in my form. In the link to the form I'm passing several values to params in the URL. The trouble comes in when I either try to pass a value to a field that is an integer or an association. In either case, the field does not pre-populate.
Example below...the first two fields populate fine, but I had to force them to be text fields. Maybe that's ok to push the strings from the url into the field, but ideally I'd be able to use either the integer (f.input) or association (f.association). The second two fields don't pull in the param values from the URL.
Any ideas? Thanks in advance!
NOTE - this is for generating a NEW record in the database and not for editing an existing record.
URL: http://localhost:5000/list/new?event_id=4&user_id=11
<!-- These two fields pre-populate -->
<%= f.text_field :event_id, :value => params[:event_id] %>
<%= f.text_field :user_id, :value => params[:user_id] %>
<br>
<!-- These two fields do NOT pre-populate -->
<%= f.association :event_id, :value => params[:event_id] %>
<%= f.input :event_id, :value => params[:event_id], label: 'Event' %>
PS - I'm listening to GusGus' new album on Spotify while working on this and it's helping a lot. :)
Best practice is pre-populate form not with params directly but with ActiveRecord object.
For example you have an AR class:
class Party < ActiveRecord::Base
belongs_to :event
belongs_to :user
end
Then in your controller:
def new
#party = Party.new(party_params)
end
# use strong params to make your parameter more secure;)
def party_params
params.permit(:event_id, :user_id)
end
and then in your edit view:
<%= simple_form_for #party do |f| %>
<%= f.association :event %>
<%= f.association :user %>
<% end %>

Why this error in Three Level nested attributes in Rails 3?

this error not display on creation of record
only come when update
please help..
Your code in edit template seems to be the source of this problem. You must have defined member's last_name without using the fields_for object.
<%= f.fields_for :member do |member| %>
<%= member.text_field :first_name %>
... # Other attributes
<%= member.text_field :last_name %> # make sure you are using the fields_for instance (member in this example) here.
<% end %>
add attribute to attr_accessible list like
attr_accessible attr1, attr2
this is rails 3 technique to avoid unwanted attribute assignments like sensitive data from form.
Is applicable while using object.update_attributes function and note with object.save.

Delete association fields in Rails 3.1 view

I am trying to make a nested model form in which I can add/delete association objects on the fly.
In Rails 3.0.x that would work properly if I created a hidden input with the "_destroy" name that when set to 1 it would have deleted the association record.
Now whenever I submit the form with the hidden input _destroy set to 1 (or true) it doesn't do anything.
Any ideas?
Thanks
Did you write:
:allow_destroy => true
?
# model.rb
accepts_nested_attributes_for :model, :allow_destroy => true
# view
<%= f.fields_for :model do |fields| %>
...
Delete: <%= fields.check_box :_destroy %>
<% end %>

Resources