1

Okay this one should be easy, which means I'm on the verge of pulling out hairs!

I'll try to keep the explanation simple.

I have a form, to create a new "case." These cases have individual "parts."

I'm trying to have a list of checkboxes so you can check off the parts you'll need, click add, and then display the titles of the parts that are included.

What's happening instead is, I select the part via the checkbox, click add, then the WHOLE LIST of items, including checkboxes, gets added to the page. I just want the selected part titles to show up.

Here are my forms:

_form.html.erb

<%= simple_form_for @case, html: { multipart: true } do |f| %>
  <%= f.input :image, as: :file %>
  <%= f.input :title, label: "Case" %>
  <%= f.input :description, label: "Parts" %>

  <%= f.simple_fields_for :parts do |part| %>
    <%= render 'part_fields', f: part %>
    <%= link_to_add_association 'Add Part', f, :parts, class: "btn btn-default add-button" %>
  <% end %>

  <%= f.button :submit %>
<% end %>

_parts_fields.html.erb

<%= f.label "Parts list" %><br />
<%= f.collection_check_boxes :title, Part.all, :id, :title do |b| %>
  <div class="collection-check-box">
    <%= b.check_box %>
    <%= b.label %>
  </div>
<% end %>

I don't know what other code I need to post, so let me know if there's anything else you need.

Also, I'd like to add quantities but am not really sure where to start with that. If someone could help with that, or just point me in the right direction, that would be greatly appreciated!

Thanks

2
  • 1
    Also, in this project I have learned that ruby case statements exist, due to some errors from my model "case." haha. Will probably start over with a different name for the cases. Shows you how much of a noob I am. :) Commented Jan 20, 2016 at 18:41
  • After chatting with @Rich we realized I needed a has_and_belongs_to_many association instead of has_many, now his solution works. :) Commented Jan 20, 2016 at 22:21

1 Answer 1

1

Looks like you're over-engineering.

Firstly, if you want to add existing parts only (IE the user cannot create any new parts), you'll be able to populate the part_ids attribute of the @case object:

<%= simple_form_for @case, html: { multipart: true } do |f| %>
  <%= f.input :image, as: :file %>
  <%= f.input :title, label: "Case" %>
  <%= f.input :description, label: "Parts" %>

  <%= f.collection_check_boxes :part_ids, Part.all, :id, :name %>
  <%= f.submit %>

<% end %>

This will only allow you to "assign" existing parts to your @case; if you wanted to create new parts, you'll have to use the f.fields_for pattern, which you're doing already.

To give you some context, the reason why all the parts are being added by Cocoon is because fields_for is meant as a way to add extra records through the parent model. So your pattern of including all parts in this process is inherently wrong:

Like form_for, it yields a FormBuilder object associated with a particular model object to a block, and within the block allows methods to be called on the builder to generate fields associated with the model object


You'd need something like this:

<%= simple_form_for @case, html: { multipart: true } do |f| %>
  <%= f.input :image, as: :file %>
  <%= f.input :title, label: "Case" %>
  <%= f.input :description, label: "Parts" %>

  <%= f.collection_check_boxes :part_ids, Part.all, :id, :name %>
  <%= link_to_add_association 'Create Part', f, :parts, class: "btn btn-default add-button" %>

  <%= f.submit %>
<% end %>

#_parts_fields.html.erb
<%= f.text_field :title %>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, Rich. It's a mashup of two other projects, so the over-engineering thing makes sense! I do only want to add existing parts. I tried your solution, then realized that the :name attribute should really be :title, fixed that and it goes through. But when I check the newly created case in the console, the parts attribute is nil, just the title and description show up. How can I make it show the parts? I want to be able to sort the cases by the parts they have, mostly to exclude cases when a certain part is out of stock. Thanks again
did you permit part_ids in your strong params?
I just did that, then realized there is no part_ids attribute. Now I'm a bit confused where to add that, would it be the part or case model? I'm sorry for the silly questions, thank you for your time and patience
params.require(:case).permit(:part_ids) in the case model -- no problem, it's great you're trying to figure it out. I don't mind answering at all :)
Okay, I have that in the case controller instead. In the console when I do @case = Case.last, part_ids is still nil
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.