我试图了解如何调整本教程以制作我可以在Rails 4应用程序中使用的轮播助手。
https://www.toptal.com/ruby-on-rails/rails-helper-bootstrap-carousel
我把帮助者作为:
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
然后我尝试在我的项目展示视图中使用它。
我有:
<% @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 %>
我有一个项目模型和一个画廊模型。协会是:
Project.rb
has_many :galleries
accepts_nested_attributes_for :galleries, reject_if: :all_blank, allow_destroy: true
Gallery.rb
belongs_to :project
Gallery表具有:image属性。
我希望旋转木马循环通过每个图像。
教程没有展示如何将轮播插入到其他模型中,因此我不确定是否需要以某种方式将其合并到图库或项目模型中。
目前,如果我尝试这样做,我会收到一条错误消息:
wrong number of arguments (given 2, expected 1)
该消息突出显示了帮助者的这一行:
def carousel_for(images)
任何人都可以看到接下来的步骤是什么让它起作用?
PRAVESH&S;建议
根据Pravesh的建议,我尝试了以下方法。我不得不评论image_description标题,因为它给出了一个未定义的方法错误(image_description是我的图库表上的一个属性。我无法弄清楚如何实现这个建议以使轮播工作。最好只显示一个文本图像链接的路径(我认为它至少是这样)。控件不起作用,后续的图像/路径不显示。
<% 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&#39;第二次建议
<% @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 %>
给出了这个错误:未定义的方法`count&#39;对于#
与我开始的
相同答案 0 :(得分:1)
你在这里发送了两个论点
<%= carousel_for(gallery.image, alt: "gallery.image_alt") %> // alt is the second parameter here
然而它只需要gallery.image作为参数。这就是为什么你会收到这样的错误:
wrong number of arguments (given 2, expected 1)
并在您看来:
<% @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>
新尝试:
<div class="item"> // for the rest of the images
<%= carousel_for(@project.galleries.order(created_at: :asc)) %>
</div>
尝试2
控制器中的
@image_urls=[]
@project.galleries.each do |gallery|
@image_urls.push(gallery.image.url) // if you are using paperclip for image upload
end
在您的视图中
删除整个do每个块并仅添加以下行
<%= carousel_for(@image_urls) %>
我相信这会有所帮助,对我来说很好用