Rails 4 - carousel helper - image gallery

2019-09-01 19:27发布

问题:

Im trying to understand how to adapt this tutorial to make a carousel helper that I can use in my Rails 4 app.

https://www.toptal.com/ruby-on-rails/rails-helper-bootstrap-carousel

I have made the helper as:

module CarouselHelper
  def carousel_for(images)
    Carousel.new(self, images).html
  end

  class Carousel
    def initialize(view, images)
      @view, @images = view, images
      @uid = SecureRandom.hex(6)
    end

    def html
      content = safe_join([indicators, slides, controls])
      content_tag(:div, content, id: uid, class: 'carousel slide')
    end

    private

    attr_accessor :view, :images, :uid
    delegate :link_to, :content_tag, :image_tag, :safe_join, to: :view

    def indicators
      items = images.count.times.map { |index| indicator_tag(index) }
      content_tag(:ol, safe_join(items), class: 'carousel-indicators')
    end

    def indicator_tag(index)
      options = {
        class: (index.zero? ? 'active' : ''),
        data: { 
          target: uid, 
          slide_to: index
        }
      }

      content_tag(:li, '', options)
    end

    def slides
      items = images.map.with_index { |image, index| slide_tag(image, index.zero?) }
      content_tag(:div, safe_join(items), class: 'carousel-inner')
    end

    def slide_tag(image, is_active)
      options = {
        class: (is_active ? 'item active' : 'item'),
      }

      content_tag(:div, image_tag(image), options)
    end

    def controls
      safe_join([control_tag('left'), control_tag('right')])
    end

    def control_tag(direction)
      options = {
        class: "#{direction} carousel-control",
        data: { slide: direction == 'left' ? 'prev' : 'next' }
      }

      icon = content_tag(:i, '', class: "glyphicon glyphicon-chevron-#{direction}")
      control = link_to(icon, "##{uid}", options)
    end
  end
end

I'm then trying to use it in my projects show view.

I have:

<% @project.galleries.order(created_at: :asc).each do |gallery| %>

                        <div class="row">
                               <div class="col-md-8"> 

                                    <%= carousel_for(gallery.image,  alt: "gallery.image_alt") %>                                    

                               </div>



                                <div class="col-md-4 colored">
                                    <p class='medium-text'><%= gallery.image_description %></p>

                                </div>    



                        </div>   
                    <% end %> 

I have a projects model and a gallery model. The associations are:

Project.rb

has_many :galleries
      accepts_nested_attributes_for :galleries,  reject_if: :all_blank, allow_destroy: true

Gallery.rb

belongs_to :project

The Gallery table has an :image attribute.

I want the carousel cycle through each image.

The tutorial doesnt go as far as showing how to plug the carousel in to other models, so I'm not sure if I need to incorporate it somehow in the gallery or project models.

At the moment, if I try this, I get an error that says:

wrong number of arguments (given 2, expected 1)

The message highlights this line of the helper:

  def carousel_for(images)

Can anyone see what the next steps are to get this working?

PRAVESH'S SUGGESTION

Taking Pravesh's suggestion, I tried the following. I had to comment image_description caption because it was giving an undefined method error (image_description is an attribute on my gallery table. I can't figure out how to implement this suggestion to get the carousel working. At best it just shows a text path to an image link (i think thats what it is at least). The controls don't work and the subsequent images/paths don't show.

<% if @project.galleries == @project.galleries.order(created_at: :asc).first%>
      <div class="item active">  
           <%= carousel_for(@project.galleries) %>
            <div class="carousel-caption">
                 <p class='medium-text'><%#= @project.galleries.image_description %></p>
            </div>
      </div>
<% else %>
     <div class="item"> 
          <%= carousel_for(@project.galleries) %>
              <div class="carousel-caption">
                   <p class='medium-text'><%#= @project.galleries.image_description %></p>
              </div>
           </div>
     <% end %>

PRAVESH'S 2nd SUGGESTION

 <% @project.galleries.order(created_at: :asc).each do |gallery| %>
      <% if gallery == @project.galleries.order(created_at: :asc).first%>
        <div class="item active">  //since you need the first image as active
         <%= carousel_for(gallery.image) %>
         <div class="carousel-caption">
            <p class='medium-text'><%= gallery.image_description %></p>
          </div>
        </div>
      <% else %>
        <div class="item"> // for the rest of the images
          <%= carousel_for(@project.galleries.order(created_at: :asc)) %>

          <div class="carousel-caption">
            <p class='medium-text'><%= gallery.image_description %></p>
          </div>
        </div>
      <% end %>
    <% end %>

Gives this error: undefined method `count' for #

Which is the same one that I started out with

回答1:

You were sending two arguments here

<%= carousel_for(gallery.image,  alt: "gallery.image_alt") %> // alt is the second parameter here

however it needs only gallery.image as parameters. That is why you were getting an error like this:

wrong number of arguments (given 2, expected 1)

and in your view:

  <% @project.galleries.order(created_at: :asc).each do |gallery| %>
      <% if gallery == @project.galleries.order(created_at: :asc).first%>
        <div class="item active">  //since you need the first image as active
         <%= carousel_for(gallery.image) %>
         <div class="carousel-caption">
            <p class='medium-text'><%= gallery.image_description %></p>
          </div>
        </div>
      <% else %>
        <div class="item"> // for the rest of the images
          <%= carousel_for(gallery.image) %>
          <div class="carousel-caption">
            <p class='medium-text'><%= gallery.image_description %></p>
          </div>
        </div>
      <% end %>
    <% end %>
  </div>

New Try :

<div class="item"> // for the rest of the images
   <%= carousel_for(@project.galleries.order(created_at: :asc)) %>
</div>

Try 2

in your controller

@image_urls=[]
@project.galleries.each do |gallery|
    @image_urls.push(gallery.image.url) // if you are using paperclip for image upload
end

in your view

delete the whole do each block and only add the below line

<%= carousel_for(@image_urls) %>

I am sure this gonna help, it works fine for me