Display multiple images of a paper clip from the database in the ROR application. many relationships
I am very new to ROR and I am building an ROR application where each product can have many images. I'm using paperclip
to upload an image.
To do this, I added a model to the application image.rb
, and now a model product.rb
has_many :images
and accepts_nested_attributes_for :images
.
This all works great with problems.
The fact is that the model product.rb
has such a relationship belongs_to :category
andbelongs_to :label
and category
and label
howhas_many :products
Before I added the model image.rb
, each product had one image attached to it. and on the page, the index.html.erb
user can see the categories lined up with the last uploaded image from each product in the category being the front image for each category.
Below is the snapshot I used to display the categories in index.html.erb
before adding the model image.rb
.
<div class="container-fluid">
<% @products.each_slice(3) do |products_group| %>
<div class="row">
<% products_group.each do |category, products| %>
<% products.each_with_index do |product, index| %>
<% if index == 0 %>
<div class="col-lg-4 col-sm-6 col-xs-12 center-block " >
<%= link_to category_path (category), { :method => 'GET' } do %>
<%= image_tag product.image.url(:medium), class: "img-responsive" %>
<% end %>
<div class="caption">
<p class="category-name" ><%= product.category.name %></p>
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
And there pages_controller.rb
was this code:
def index
@products = Product.all.order(created_at: :desc).group_by(&:category_id)
end
As you can see from the code examples above, I am grouping products by their categories and then showing each category that has any products on the index page. The category shown on the index page only shows the last loaded product in each category.
But now that I have a model image.rb
to handle uploading photos, I need a way to show the same result on the index page, but when the user hits any category, they land on that category page.
This is a problem because now the images are bound to the model image.rb
, but not to the model product.rb
.
I can view the uploaded photos in the model image.rb
, but I'm not sure how I should set up a breaking everything relationship.
Can anyone help me?
below are the models:
product.rb
class Product < ActiveRecord::Base
acts_as_list :scope => [:category, :label]
belongs_to :category
belongs_to :label
has_many :images
accepts_nested_attributes_for :images
has_many :product_items, :dependent => :destroy
end
the category.rb
class Category < ActiveRecord::Base
has_many :products, :dependent => :nullify
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
end
and image.rb
class Image < ActiveRecord::Base
belongs_to :product
has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
source to share
Before you have one image in each product. And now you have a lot of images in the product. I think you just need to iterate before the image_tag:
Before:
<%= image_tag product.image.url(:medium), class: "img-responsive" %>
After:
<% product.images.each do |image_model| %>
<%= image_tag image_model.image.url(:medium), class: "img-responsive" %>
<% end %>
If you want to show the first image:
<% if product.images.first %>
<%= image_tag product.images.first.image.url(:medium), class: "img-responsive" %>
<% end %>
source to share