回形针,得到 "undefined method error" :photo
Paperclip, getting "undefined method error" :photo
我在 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 存储桶中,只是没有使用此方法在我的视图中呈现。
你的 view
有错字:
@products
是一个 array of objects
而不是可以在 object
.
上调用 photo
的对象
请相应地更改您的代码您可以通过调用 @products[0].photo.url
获取照片
我在 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 存储桶中,只是没有使用此方法在我的视图中呈现。
你的 view
有错字:
@products
是一个 array of objects
而不是可以在 object
.
photo
的对象
请相应地更改您的代码您可以通过调用 @products[0].photo.url