> On Feb 8, 2018, at 8:28 AM, fugee ohu <fugee...@gmail.com> wrote:
> I was having trouble hiding and showing elements on the form so I created a 
> separate action in the pictures controller to handle the submission of the 
> form that selects pictures to add to the person from existing pictures in the 
> database It attempts to create a new picture I guess because validation fails 
> :name cannot be blank
> def add_from_pictures_create
>            if (params[:picture][:person_id])
>                @person=Person.find(params[:picture][:person_id])
>                @pictures = Picture.find(params[:person][:picture_ids])

# these are now new instances -- with different object IDs -- than the ones you 
want to compare them with

>                @person.pictures << @pictures

# assigning these in this way means that you are adding an array to an array. 
Trouble is, you are adding duplicate instances in the second array to an array 
which may already contain the ones you want to add.

>                respond_to do |format|
>                  if @person.save
>                             format.html {  redirect_to 
> pictures_path(person_id: params[:picture][:person_id]), notice: 'Person 
> picture updated.' }
>                             format.json { render :show, status: :created, 
> location: @picture } 

I would avoid this entire approach. Think about this in the abstract: you are 
editing and updating the @person, manipulating the picture_ids attribute on 
that instance. Your form is built on the person. This form should be submitted 
to the PersonController#update method. If you have (as I have said many times 
in this thread) whitelisted the picture_ids attribute, then simply calling 
@person.save will persist that attribute -- it is already going to be in the 
person_params strong parameters hash.

Here's a clue: when you find yourself writing something like this in a 

> @person=Person.find(params[:picture][:person_id])

...just walk away and have a think about your life.


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 
For more options, visit https://groups.google.com/d/optout.

Reply via email to