【发布时间】:2013-05-01 16:57:15
【问题描述】:
我正在使用标准 User 模型构建一个简单的应用程序,该模型具有 has_one Profile 模型。我希望配置文件表有一个图像属性列。因此,我遵循了 RailsCast #253,标题为 CarrierWave File Uploads。一切都很顺利,直到我尝试调整已上传的图像大小。这需要安装 ImageMagick 和 RMagick,这需要一整天的搜索才能完成。不过,我想我终于做对了,并成功安装了 rmagick 版本 2.13.2(通过运行“gem list”验证)。
但不是那么快...现在当我尝试渲染表单以创建新配置文件时,我收到以下错误:
配置文件中的 NoMethodError#new NilClass:Class 的未定义方法“model_name”
仅供参考,在我在 ImageUploader 中取消注释“包括 CarrierWave::RMagick”之前,此表单运行良好(如果我想使用 RMagick 方法调整图像大小,我应该这样做)。
关于如何解决这个问题的任何想法?
我的版本信息(我使用 RailsInstaller for Windows 启动和运行)
Rails 3.2.13
Ruby 1.9.3p392 [i386-minw32]
ImageMagick 6.8.5-Q16
rmagick 2.13.2
宝石文件
gem 'rmagick'
gem 'carrierwave'
型号
class User < ActiveRecord::Base
has_one :profile
class Profile < ActiveRecord::Base
attr_accessbile :image
belongs_to :user
mount_uploader :image, ImageUploader
图片上传
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
ProfilesController
def new
@profile = current_user.build_profile
end
views/profiles/new.html.erb
<%= form_for @profile, :html => {:multipart => true} do |f| %>
<%= f.file_field :image %>
<%= f.submit "Create my profile" %>
<% end %>
【问题讨论】:
标签: ruby-on-rails-3 rmagick railscasts