Rails displaying images from a belongs_to model

I need to display multiple images for a a car from a separate linked table, i have the both models sorted with the belongs_to and has_many but on the main car page i need to show images from the linked table that have the same id.

here is the current code:-

<p id="notice"><%= notice %></p>


<h2>
  <em><%= @car.make.make_name %> <%= @car.model %> <%= @car.body_typw %> <%= @car.engine_size %> <%= @car.trim %></em>
</h2>

<p>
  <%= image_tag @car.image(:large) %>
</p>

<% @carimages.each do |carimage| %>

    <%= image_tag carimage.image(:thumb), :class => "imgsmall" %>

<% end %>

<p>
  <b>Transmission:</b>
  <%= @car.transmission %>
</p>

<p>
  <b>Fuel type:</b>
  <%= @car.fuel_type %>
</p>

<p>
  <b>Millage:</b>
  <%= @car.millage %>
</p>

<p>
  <b>Price:</b>
  <%= number_to_currency(@car.price) %>
</p>

<p>
  <%= raw @car.content %>
</p>

<p style="font-size:16px;"><b>Call us now on <span style="color:red;">01446 746 785</span> to find out more about the <%= @car.model %> <%= @car.body_typw %> <%= @car.engine_size %> <%= @car.trim %></b></p>

so basically the url is - http://localhost:3000/cars/1 and i want the car images with the linked id of car_id of 1 to be shown.

Robbie

Answers


Assuming your car images table is named 'carimages' and you have the right belongs_to association in this table, and has_many in you 'cars' table, you can always call

@car.images.each do |image|

to get all the images linked whit you @car


Need Your Help

Re-populating dropdown menu with database value (rails)

ruby-on-rails ruby drop-down-menu edit populate

When going to the Edit view of a form, with values previously posted to a database, all the fields get re-populated except for the drop-down menu field. For example, the "Surprised" value is now i...