【问题标题】:Ruby on rails image gallery using paperclip使用回形针的 Ruby on rails 图像库
【发布时间】:2014-11-25 11:07:59
【问题描述】:

我正在创建一个图片库网站,但遇到了一些问题。我试过搜索,但很难,因为我无法真正描述这个问题。

我想在我的索引页上显示缩略图,但我无法正常工作。

为什么这段代码在 /views/index.html.erb 里面

    <% @galleries.each do |gallery| %>
    <%= image_tag gallery.photos(:small).last %>
    <%= link_to gallery.title, gallery_path(gallery) %>

不显示照片,只是显示照片应该出现的空白区域。当我右键单击它们以在新选项卡中打开时,它链接到 /images/#..... 并给出 No routes 错误。

在 show.html.erb 图片上用这个代码显示:

      <% @gallery.photos.each do |photo|%>
      <%= image_tag photo.image(:medium) %>
     <% end %>

模型是:画廊有很多照片,照片属于画廊。

画廊控制器: 定义索引 @galleries = Gallery.all.order(created_at: :desc) 结束

    def show
        @gallery = Gallery.find(params[:id])
    end

路线: 资源:画廊 资源:照片

【问题讨论】:

    标签: ruby-on-rails image paperclip gallery


    【解决方案1】:

    @rubynuby:试试这个:

    <% @galleries.each do |gallery| %>
    <%= image_tag gallery.photos.last.image.url(:small) %>
    <%= link_to gallery.title, gallery_path(gallery) %>
    

    我从你的问题中观察到的是:

    在models/gallery.rb中

    class Gallery
      has_many :photos
    end
    

    在models/photo.rb中

    class Photo
      belongs_to :gallery
      has_attached_file :image, styles: {
                          small:  '150x150>',
                          medium: '600X600>'
                        }
    
    end
    

    因此,根据您的要求,您需要在照片上致电image,即photo.image.url(&lt;style&gt;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 2017-01-14
      • 2016-05-30
      • 1970-01-01
      • 2015-11-14
      • 2015-06-26
      相关资源
      最近更新 更多