Raza
Raza

Reputation: 2388

Customizing Spree Home Page Model

I have an image slider at home page(Spree). I want to populate the slider with images from database. For that i have to create an image model, and associate it with home model. I found home controller and views, but I couldn't find home model. Where can i find it? and is this the right way of doing it, or is there any other optimized method?

Upvotes: 1

Views: 331

Answers (1)

Raza
Raza

Reputation: 2388

Ok, I solved it as, define a model for images inside Spree module

module Spree

 class SliderImage < Spree::Base
  has_attached_file :image, :styles => { :large => "980 x 280>",:medium => "400x100>", :thumb => "100x50>" }, :default_url => "no-photo.png"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
 end

end

Make a tab for SliderImage in admin panel, where admin can deal with the slider images. Then, define its instance variable in home controller

module Spree
 class HomeController < Spree::StoreController
  respond_to :html

   def index
    @slider_images = Spree::SliderImage.all
   end
 end
end 

Now you can dynamically load your images in slider.

   #../app/views/spree/home/index.html.erb
   .......

      <% @slider_images.each do |image_slider| %>
        <div class="item active">
            <%= image_tag(image_slider.image(:large), alt: image_slider.title, class: "img-responsive", style: "width: 100%;") %>
        </div>
     <% end %>

   ........

Upvotes: 1

Related Questions