【问题标题】:dragonfly rails 4 image_tag error蜻蜓导轨4 image_tag错误
【发布时间】:2015-10-25 16:18:09
【问题描述】:

我正在使用 Rails 4.1.6 和 Ruby 2.0.0p576 构建我的摄影作品集。

我有很多“照片”的“收藏”。

我正在使用 Dragonfly 1.0.10 进行图片上传。

集合模型

    class Collection < ActiveRecord::Base
      belongs_to :admin
      has_many :photos
    end

照片模型

   class Photo < ActiveRecord::Base
    belongs_to :collection
    extend Dragonfly::Model
    dragonfly_accessor :image
   end

收集控制器

class CollectionsController < ApplicationController
 respond_to :html, :xml, :json

 def index
  @collections = Collection.all
 end

 def new
  @collection = Collection.new
  respond_with (@collection)
end

 def create
  @collection = Collection.new(collection_params)
  @collection.save
  respond_with (@collection)
 end

 def edit
  @collection = Collection.find(params[:id])
 end

 def update
  @collection = Collection.find(params[:id])

  if @collection.update(collection_params)
   redirect_to @collection
  else 
   render 'edit'
  end    
 end

 def show
  @collection = Collection.find(params[:id])
 end

 def destroy
  @collection = Collection.find(params[:id])
  @collection.destroy
  respond_with (@collection)
 end

 private
  def collection_params
   params.require(:collection).permit(:name, :desc, photos: [:image] )
  end

end

照片控制器

    class PhotosController < ApplicationController

     def new
      @collection = Collection.find(params[:id])
      @photo = @collection.new
     end

     def create
      @collection = Collection.find(params[:collection_id])
      @photo = @collection.photos.create(photo_params)
      redirect_to collection_path(@collection)
     end

     private
      def photo_params
       params.require(:photo).permit(:image)
      end

   end

这是为我正确存储我的照片。

但在我的收藏展示页面上,我收到“收藏中的 NoMethodError#show undefined method `image'”。

在终端中,我收到以下错误: ActionView::Template::Error(未定义的方法 `image' for #):

显示页面的代码如下:

collections/show.html.erb

    <h1><%= @collection.name %></h1>

    <p><%= @collection.desc %></p>

    <% @collection.photos.each do |photo| %>
     <div>
      <%= image_tag @collection.photos.image.thumb('400x300#').url %>
     </div>
    <% end %>  

我是一个绝对的 Rails n00b,需要一些帮助来解决这个问题,并能够在其展示页面上查看该系列的所有照片。

请帮忙!

【问题讨论】:

  • 试试这个&lt;%= image_tag photo.image.thumb('400x300#').url %&gt;
  • 谢谢帕万!这对我有用!

标签: ruby-on-rails ruby dragonfly-gem


【解决方案1】:

您对each 循环的使用有误:

<% @collection.photos.each do |photo| %>
   <div>
     <%= image_tag photo.image.thumb('400x300#').url %>
   </div>
 <% end %>  

Read How does iteration work in Ruby?

【讨论】:

  • 感谢您的帮助!!我一定会阅读更多内容。
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多