Ruby on Rails
Monday, February 5, 2018
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> wrote:
>
> So if @person.pictures << @picture adds a picture to a person, how do you remove a picture from a person?
>
>
Yea, that's pretty good thanks What about the gem that let you use images in select lists I would think a lot of images is too many for a select list
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+unsubscribe@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/309c1316-c831-43f6-8bc2-951e97d9f3ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment