【发布时间】:2018-10-15 18:34:57
【问题描述】:
我在尝试允许通过 Shrine 上传头像时遇到了一些问题。我正在使用 Rails 5。我不断收到错误消息“nil:NilClass 的未定义方法 `cached_image_data'”。
我尝试过多次重启,并按照各种教程进行操作。据我所知,我正在做我应该做的一切。 ImageUploader 已设置,我的照片模型已设置等。我在下面包含了相关的代码。
apps/models/photo.rb:
class Photo < ApplicationRecord
include ImageUploader::Attachment.new(:image)
end
apps/uploaders/image_uploader.rb 需要“image_processing/mini_magick”
class ImageUploader < Shrine
plugin :processing
plugin :versions, names: [:original, :thumb, :medium]
plugin :delete_raw # delete processed files after uploading
process(:store) do |io, context|
original = io.download
pipeline = ImageProcessing::MiniMagick.source(original)
size_80 = pipeline.resize_to_limit!(80, 80)
size_300 = pipeline.resize_to_limit!(300, 300)
original.close!
# return hash of 3 sizes of the same image
{ original: io, thumb: size_80, medium: size_300 }
end
结束
app/views/profiles/_form.html.erb
<%= form_with(model: profile, local: true) do |form| %>
<div class="field">
<%= form.label :image %>
<%= form.hidden_field :image, value: @photo.cached_image_data %>
<%= form.file_field :image, id: :photo_image_data %>
</div>
<% end %>
app/controllers/profile_controller.rb
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
before_action :view_own_profile, only: [:show]
def show
end
private
def set_profile
@profile = Profile.find(params[:id])
end
def profile_params
params.require(:profile).permit(:first_name, :last_name, :mobile,
:street_address, :suburb, :postcode, :country, :mobile, :image,
:latitude, :longitude, :user_id)
end
结束
任何帮助都将不胜感激。如果需要任何其他信息,请告诉我。
干杯, 创
【问题讨论】:
标签: ruby-on-rails ruby