【问题标题】:Paperclip, getting "undefined method error" :photo回形针,得到“未定义的方法错误”:照片
【发布时间】:2015-11-23 19:43:03
【问题描述】:

我在 s3 上存储了一张照片,当我尝试显示照片时,未定义该方法。

为什么 has_attached_file 没有在这里定义 :photo?

我的模特:

has_attached_file :photo,
                  :styles => { :medium => "300x300>", 
                  :thumb => "100x100>" },
                  :storage => :s3,
                  :s3_credentials => "#{Rails.root}/config/aws.yml",
                  :s3_region => 'US Standard'

我的控制器:

@products = Store.all

private
  def product_params
    params.require(:store).permit(:id, :name, :photo)
  end

我的看法:

<%= image_tag @products.photo.url %>

我的架构(只是为了显示回形针迁移有效):

create_table "stores", force: :cascade do |t|
    t.string   "name",               limit: 255

    t.string   "photo_file_name"
    t.string   "photo_content_type"
    t.integer  "photo_file_size"
    t.datetime "photo_updated_at"
  end

我忘了提一下:我的表单正在使用 :photo 参数将照片保存到我的 S3 存储桶中,它只是没有使用这种方法在我的视图中呈现。

【问题讨论】:

    标签: ruby-on-rails amazon-s3 paperclip


    【解决方案1】:

    这是您的view 中的一个错字:

    @products 是一个 array of objects 不是你可以在 object 上调用 photo 的对象。

    请相应更改您的代码,您可以致电@products[0].photo.url获取照片

    【讨论】:

    • 实际上我在 Store 模型中...我只有一个 Store 的 @products 实例。
    • 你太棒了!谢谢你!我知道这是一个数组,但不确定回形针在做什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多