Rails 4 – 轮播助手 – 图片库
我试图了解如何调整本教程,以制作我可以在我的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.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的建议为例,我尝试了以下内容。 我不得不评论image_description标题,因为它给出了一个未定义的方法错误(image_description是我的图库表上的一个属性。我无法弄清楚如何实现这个建议以使轮播工作。最好它只显示一个文本路径一个图像链接(我认为这至少是它)。控件不起作用,后续图像/路径不显示。
PRAVESH的第二个建议
//since you need the first image as active // for the rest of the images
给出了这个错误:#的未定义方法`count’
这与我开始时的情况相同
你在这里发了两个论点
<%= 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%> //since you need the first image as active <%= carousel_for(gallery.image) %> <% else %> // for the rest of the images <%= carousel_for(gallery.image) %> <% end %> <% end %>
新尝试:
// for the rest of the images <%= carousel_for(@project.galleries.order(created_at: :asc)) %>
试试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) %>
我相信这会有所帮助,它对我来说很好
- 在部署之前检查具有local:path的gems
- Rails,没有密码的Authlogic(注册确定.user_sessionvalidation失败)
- Rails 5+中OO / Tableinheritance(即STI,MTI,CLI)的可能解决方案是什么?
- NameError:has_many通过关系的未初始化常量
- Rails 4嵌套属性不保存
- 在给定私钥和消息的情况下,不确定如何生成ECDSA签名
- RESTful Web服务与数据密集型应用程序的套接字编程
- AdminController中的AbstractController :: DoubleRenderError
- 升级到ruby 2.3.0 NameError:uninitialized constant I18n :: Config :: Backend