On Thursday, February 8, 2018 at 11:36:47 AM UTC-5, fugee ohu wrote:
>
>
>
> On Thursday, February 8, 2018 at 11:10:29 AM UTC-5, Walter Lee Davis wrote:
>>
>>
>> > On Feb 8, 2018, at 8:28 AM, fugee ohu <fuge...@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 
>> controller: 
>>
>> > @person=Person.find(params[:picture][:person_id]) 
>>
>> ...just walk away and have a think about your life. 
>>
>> Walter 
>>
>>
> I created an update action in the persons controller Valitadation was 
> failing with :name can't be blank so I assumed @person.save was trying to 
> create a new picture That's why I moved the action from the pictures 
> controller to the persons controller and changed the action to 
> @person.update instead of @person.save Did you already understand that?
>

You wanted me to use patch or post to achieve @person.pictures << @pictures 
and then I would wanna use if @person.save or if @person.update Please 
clarify that Thanks in advance  I took out the stuff left over from when 
this routine was in the pictures controller and now rely on the callback 
set_person 

-- 
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/40052a2b-87bd-40be-ac63-510da6b1c6d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to