On Monday, February 5, 2018 at 11:46:24 AM UTC-5, Walter Lee Davis wrote: > > You (effectively) delete the intermediate object. You could try one of two > things: > > @person.pictures -= [@picture] > > If all the items are persisted, then that should delete the join object > immediately. (Just tested, it did work here.) > > @person.pictures = @person.pictures.to_a.reject{ |p| p == @picture } > > (Long-hand way to do the same thing) > > Now if you have a UI around this, what you would do is build an array of > checkboxes with the picture IDs in them, and then use the helper method > @person.picture_ids=(array of ids) that the association built for you. You > don't need to do any of the above long-hand. > > So in your controller, you would add picture_ids: [] to the end of your > list of allowed attributes in the strong parameters. Next, you would create > a checkbox for each attached image in your form: > > <%- @person.pictures.each do |picture| %> > <%= check_box_tag 'person[picture_ids][]', picture.id, true %> > <%= image_tag picture.file_url %> (just guessing how your internals look, > do something here to show a thumbnail) > <%- end %> > > And that should do the whole thing for you. > > Walter > > > On Feb 5, 2018, at 1:18 AM, fugee ohu <fuge...@gmail.com <javascript:>> > wrote: > > > > So if @person.pictures << @picture adds a picture to a person, how do > you remove a picture from a person? > > > > > > I dunno why this doesn't produce any output
<% @pictures=Picture.all %> <% @pictures.all do |picture| %> <% if @person %> <% if picture.person_id == @person.id %> <%= check_box_tag 'person[picture_ids][]', picture.id, true %> <% else %> <%= check_box_tag 'person[picture_ids][]', picture.id %> <% end %> <%= image_tag picture.name.thumb %> <% end %> <% end %> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/494ba1a7-b2ba-4f3c-a26f-6f586c63fb3d%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.