the problem is that I have a form that normally should render photos of current car and it does so but it renders them multiple times.. like this: I have 5 photos for car_id 2 and on screen I have 5 times these photos displayed.
12345
12345
12345
12345
12445
If I add a photo, then it will be diplayed 6 times. what is the problem and how can I solve it, I’ve tried a lot of: replace this, replace that but nothing.
Here is a piece of the form itself
<%= form_for @car, :html => { :method => :put }, :html => { :multipart => true } do |f| %>
...
...
<% for photo in @car.uploads %>
<div style="width:750px !important;">
<%= f.fields_for :uploads do |photo_fields| %>
<%unless photo_fields.object.new_record? %>
<div style="width:150px;float:left;">
<%= image_tag(photo_fields.object.photo.url(:thumb)) %>
<p><%= photo_fields.check_box :_destroy %> Delete item</p>
</div>
<% end %>
<% end %>
</div>
....
...
<% end %>
and the console:
Started GET "/cars/2/edit" for 127.0.0.1 at Sun Nov 06 22:50:17 +0200 2011
Processing by CarsController#edit as HTML
Parameters: {"id"=>"2"}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
SQL (4.1ms) SHOW TABLES
Car Load (0.2ms) SELECT `cars`.* FROM `cars` WHERE `cars`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `cars`.* FROM `cars` WHERE `cars`.`id` = 2 LIMIT 1
Carname Load (0.2ms) SELECT `carnames`.* FROM `carnames`
Carmodel Load (0.2ms) SELECT `carmodels`.* FROM `carmodels`
Rendered shared/_error_messages.html.erb (1.0ms)
Upload Load (0.2ms) SELECT `uploads`.* FROM `uploads` WHERE (`uploads`.car_id = 2)
Carname Load (0.2ms) SELECT `carnames`.* FROM `carnames` WHERE `carnames`.`id` = 1 LIMIT 1
Carmodel Load (0.2ms) SELECT `carmodels`.* FROM `carmodels` WHERE `carmodels`.`id` = 5 LIMIT 1
Rendered uploads/_upload.html.erb (8.1ms)
Rendered cars/_show.html.erb (2.7ms)
WARNING: Can't mass-assign protected attributes: user_id
Rendered cars/_form.html.erb (460.6ms)
Rendered welcome/_sliderempty.html.erb (1.0ms)
Rendered welcome/_footer.html.erb (0.5ms)
Thanks for your time.
just remove the for loop,
fields_foralready iterates over the photos so you iterate twice you get the photos twice.In response to your comment: try to pass the collection explicitly like:
<%= f.fields_for :uploads, @car.uploads do |photo_fields| %>So here you go: