Handle Check Box Forms with an ':Has_Many :Through' Record Association

Handle check box forms with an `:has_many :through` Record Association

The link Dogbert supplied is a good way of doing it manual. If you however decide to use Simple Form you get that functionality build in.

Using the example from the previously mentioned article you would do:

<%= simple_form_for(@user) do |f| %>
<%= f.association :groups, :as => :check_boxes %>
<%= f.submit %>
<% end %>

This example is of course very simplified but it should work. Good luck!

Handle check box forms with an `:has_many :through` Record Association

The link Dogbert supplied is a good way of doing it manual. If you however decide to use Simple Form you get that functionality build in.

Using the example from the previously mentioned article you would do:

<%= simple_form_for(@user) do |f| %>
<%= f.association :groups, :as => :check_boxes %>
<%= f.submit %>
<% end %>

This example is of course very simplified but it should work. Good luck!

Rails Has Many Through Polymorphic Checkboxes

Okay I figured this one out. I'm answering here so I can hopefully help someone someday, but the problem was a basic oversight on the difference between Has Many Through and Has And Belongs To Many Associations.

You if you want to handle checkboxes or multiselect lists in a Has Many Through, there is no '_ids' method generated for you by the association and you need to write one for yourself.

See Paul Barry's article here:
http://paulbarry.com/articles/2007/10/24/has_many-through-checkboxes

Polymorphic stuff can be complicated, but if you're stuck real good maybe take a step back and make sure it's not something even more basic that's messing you up-- worked for me!

Below is the code modified from Paul's blog on how to handle this if you're dealing with a polymorphic has many through (basically you just need to use the attributes hash and write your polymorphic_type attribute):

attr_accessor :store_ids
after_save :update_stores

#after_save callback to handle group_ids
def update_stores
unless store_ids.nil?
self.staffings.each do |staffing|
staffing.destroy unless store_ids.include?(staffing.staffable_id.to_s)
store_ids.delete(staffing.staffable_id.to_s)
end
store_ids.each do |s|
self.staffings.create(attributes = {:staffable_id => s, :staffable_type => 'Store'}) unless s.blank?
end
reload
self.store_ids = nil
end
end

rails 3 has_many :through Form with checkboxes

I may be wrong, but I think that the first arg of the check_box_tag function is the actual name of the input, so instead of

check_box_tag :company_ids, company.id, @user.companies.include?(company), :name => 'user[company_ids]'

you could try something like

check_box_tag 'user[company_ids]', company.id, @user.company_ids.include?(company.id)

Let me know if it works!

Update multiple checkboxes on association model through nested attributes | Cocoon gem | Rails

This does not render the form correctly, since you are not actually iterating over nested rows, that is why the id is 0 for all rows.

Instead I would precreate all predefined rules for a house in your controller, in the new action. As follows:

def new 
@house = House.new
HouseRule.predefined.each do |hr|
@house.house_rules.build(rule: hr.rule)
end
end

And in your view just iterate over the house-rules as usual:

<%=  f.simple_fields_for :house_rules do |house_rule| %>
<%= house_rule.label :allowed, house_rule.object.rule %>
<%= house_rule.check_box :allowed, {}, 'yes', 'no' %>
<% end %>

How do I find out why I couldn't #destroy() a record?

Had the exact same error right now. I didn't get any backtrace, because it was a logic error.

In my case my models had a conflicting dependent: destroy in the relations in the model. That means my model wanted to delete a parent model which was already deleted before hand.

I think it's a bit confusing, i'll make it clear by declaring the models:

class Section < ApplicationRecord
has_many :contents, dependent: :destroy

class Content < ApplicationRecord
belongs_to :section
has_many :quizzes, dependent: :destroy

class Quiz < ApplicationRecord
belongs_to :content, dependent: :destroy

I deleted the Section section.destroy and got the error. The error was the dependent: destroy in the relation :content in the Quiz-model, because the content was already deleted by the dependent: destroy in the section model. I hope this will help to find the problem :)

How do I update a simple_form input field using AJAX?

The problem was I didn't know how to update just one element inside a simple_form.
I also had a very loose grasp on using AJAX with rails not knowing you could have both HTML and JS partial views for the same controller, as such I was trying to fit all my code in just HTML which would have been cumbersome and going against unobtrusive JavaScript.

Stumbling upon the jQuery method replaceWith was also very helpful.

Armed with this knowledge, I was able to accomplish what I had set out to do.

I created a JavaScript partial view which is rendered in response to the AJAX call. The partial renders a .html.erb partial view which rebuilt a simple_form with the updated element. The JS view then extracts the updated element from the rebuilt form and uses the jQuery replaceWith() method to replace just the specific element inside the initial form while leaving the rest of the form unchanged.

Here is how the JS partial (location.js.erb) looks like:

var updatedLocations = $('#product_location_id', $('<%= j render "updateLocations" %>'))
$('#product_location_id').replaceWith(updatedLocations)

And the HTML erb partial (updateLocations.html.erb) looks something like this:

<%= simple_form_for @product, html: {class: 'form-horizontal'} do |f| %>

<%= f.association :location, collection: @locations, input_html: { data: {update_locations: "true", remote: :true, url: "/update_locations"} } %>

<% end %>

And the controller:

  def update_locations
@product = Product.new
@location = Location.find_by_id(params[:product][:location_id])
@locations = @location.children
if @locations.empty?
render nothing: true
else
render partial: 'locations'
end
end


Related Topics



Leave a reply



Submit