0

Iam new to rails 4 i want add multiple images.i have a product model and picture model in my app.product has many picture and picture has a image and its a paperclip attachment but i cant access images in my view models/products.rb

class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments , dependent: :destroy
has_many :pictures, :dependent => :destroy
end

models/picture.rb

class Picture < ActiveRecord::Base
belongs_to :product
accepts_nested_attributes_for :images,:allow_destroy => true
has_attached_file :image,:styles => {
:thumb    => ['100x100#',  :jpg, :quality => 70],
:preview  => ['480x480#',  :jpg, :quality => 70],
:large    => ['600>',      :jpg, :quality => 70],
:retina   => ['1200>',     :jpg, :quality => 30]
},
:path => ":rails_root/public/images/:id/:filename",
:url  => "/images/:id/:filename"

do_not_validate_attachment_file_type :image
end

View page

<div class="row">
                <% @products.each do |product| %>
                <div class="col-sm-3  col-lg-3 col-md-3">
                    <div class="thumbnail">

                        <%= product.image.url(:thumb) %>
                        <div class="caption">
                            <h4 class="pull-right">Rs.<%= product.price %>   </h4>
                            <h4><%= link_to 'Show', product %>
                            </h4>
                            <p><strong>Name:</strong>&emsp;  <%= product.name %></span></p>
                            <% if !product.description.blank? %>
                                <p><strong>Description:</strong> <%= product.description %></p>
                            <% end %>
                            <% if !product.reason.blank? %>
                                <p><strong>Reason:</strong><%= product.reason %></p>
                            <% end %>

                        </div>

                    </div>
                </div>
                <% end %>
</div>   
2
  • are you getting a specific error while trying to access the images? what's happening on the front end? Let's see the error Commented Dec 25, 2015 at 5:50
  • unkown method image in product Commented Dec 25, 2015 at 7:44

2 Answers 2

1

You're trying to call a nested method on your parent model.

You have:

#app/models/product.rb
class Product < ActiveRecord::Base
   has_many :pictures
end

#app/models/picture.rb
class Picture < ActiveRecord::Base
   belongs_to :product
   has_attached_file :image
end

This means that you will have to call:

<% @products.each do |product| %>
   <% product.pictures.each do |picture| %>
      <%= picture.image.url %>
   <% end %>
<% end %>

--

You're receiving a noMethod error because you're trying to call image on your Product object (which doesn't exist):

<%= picture.image.url #-> doesn't exist %>

Fix

The fix is a little more involved than you realize.

Specifically, you have to ensure that you're able to add/create the nested Picture objects, which comes from the accepts_nested_attributes_for method:

#app/models/product.rb
class Product < ActiveRecord::Base
   has_many :pictures
   accepts_nested_attributes_for :pictures 
end

This will allow you to create the appropriate form & controller actions:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
   def new
      @product = Product.new
      @product.pictures.build
   end

   def create
      @product = Product.new product_params
      @product.save
   end

   private

   def product_params
      params.require(:product).permit(..., pictures_attributes: [:image])
   end
end

You'll then use fields_for:

#app/views/products/new.html.erb
<%= form_for @product do |f| %>
   <%= f.fields_for :pictures do |p| %>
     <%= p.file_field :image %>
   <% end %>
   <%= f.submit %>
<% end %>

This will allow you to save new Picture's / images with a new product, then using the code above, you'll be able to show them!!


Bonus

A pro tip I keep sharing is paperclip_defaults:

#config/application.rb
...
config.paperclip_defaults = {
   :styles => {
       :thumb    => ['100x100#',  :jpg, :quality => 70],
       :preview  => ['480x480#',  :jpg, :quality => 70],
       :large    => ['600>',      :jpg, :quality => 70],
       :retina   => ['1200>',     :jpg, :quality => 30] #-> is this correct formatting?
   },
   :path => ":rails_root/public/images/:id/:filename",
   :url  => "/images/:id/:filename"
}

Instead of including all the styles in your Picture model, you can set them as "defaults" in your config/application.rb - which will allow you to just call has_attached_file :image, changing the styles as you require:

#app/models/picture.rb
class Picture < ActiveRecord::Base
   has_attached_file :image #-> will use the defaults which can be overridden as required
end
Sign up to request clarification or add additional context in comments.

Comments

0

Please try this:

<% for asset in @picture.assets %>
 <%= link_to image_tag(asset.image.url(:thumb)), asset.image.url(:original) %>
<% end %>

I hope it will help you. You can also follow below link:

https://github.com/Emerson/Multiple-File-Uploads-with-Paperclip-and-Rails-3/blob/master/app/views/posts/show.html.erb

1 Comment

i'm having this error \n Mysql2::Error: Unknown column 'pictures.product_id' in 'where clause': SELECT pictures.* FROM pictures WHERE pictures.product_id = 9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.