Hi, I have a product that has many photos, so when creating a new product the user can upload as many photos as they want. This all works fine using nested attributes for. I want the user to be able to set one of the photos as the default/primary photo, so i have played around with using a radio button to decide which photo to select as the default but i cant seem to figure out how to save this to the database because the records dont exists yet and i have no id field to go on. Anyone got any solutions or ideas, ive done this before successfully but the data has always come from an existing table with data in it. JB -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If I''m understanding correctly, you''re allowing the user to upload a set of photos and at the same time you''re allowing the user to specify which of those photos should be the default. Could you do something like this, <input type="file" name="product[photos_attributes][0][photo]"><input type="radio" name="product[default_photo]" value="0"> <input type="file" name="product[photos_attributes][1][photo]"><input type="radio" name="product[default_photo]" value="1"> <input type="file" name="product[photos_attributes][2][photo]"><input type="radio" name="product[default_photo]" value="2"> In other words, use the "index" as the value of the selected photo. Another alternative is to use a bit of js along the following lines, <input type="file" name="product[photos_attributes][0][photo]"><input type="radio" name="product[photo_attributes][0][default]" value="1" onclick="uncheck the others"> <input type="file" name="product[photos_attributes][1][photo]"><input type="radio" name="product[photo_attributes][1][default]" value="1" onclick="uncheck the others"> <input type="file" name="product[photos_attributes][2][photo]"><input type="radio" name="product[photo_attributes][2][default]" value="1" onclick="uncheck the others"> On Oct 10, 5:50 am, johnnybutler7 <johnnybutl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a product that has many photos, so when creating a new product > the user can upload as many photos as they want. This all works fine > using nested attributes for. I want the user to be able to set one of > the photos as the default/primary photo, so i have played around with > using a radio button to decide which photo to select as the default > but i cant seem to figure out how to save this to the database because > the records dont exists yet and i have no id field to go on. > > Anyone got any solutions or ideas, ive done this before successfully > but the data has always come from an existing table with data in it. > > JB-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, You can solved in the view or in the model, also depends where you place the default. Some ideas: 1. If default is in the product, in the product model add "after_save", find the photo, get de id and save id in the product. 2. If default is in the photo, solved it in the view. Have you consider changing attribute default (boolean) for position (integer)? And in this case first position will be default, and you can always reorganize it in the way you want. Greetings, El 10/10/2011 14:50, johnnybutler7 escribió:> Hi, > > I have a product that has many photos, so when creating a new product > the user can upload as many photos as they want. This all works fine > using nested attributes for. I want the user to be able to set one of > the photos as the default/primary photo, so i have played around with > using a radio button to decide which photo to select as the default > but i cant seem to figure out how to save this to the database because > the records dont exists yet and i have no id field to go on. > > Anyone got any solutions or ideas, ive done this before successfully > but the data has always come from an existing table with data in it. > > JB >-- Miquel Cubel Escarré +34 699 73 22 46 mcubel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org "Computers are good at following instructions, but not at reading your mind." Donald Knuth. "Los ordenadores son buenos siguiendo instrucciones, pero no leyendo tu mente." Donald Knuth. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.