Carrierwave:store_path 中的 model.id 错误
我正在尝试编写一种方法来将给定网址中的图像存储在 ruby 工作线程中。它随我的 Rails 应用程序一起提供,我在其中显示对象图像。这是我想到的:
@message = Message.create!
my_uploader = PhotoUploader.new
photo = open(image_url)
@message[:photo] = my_uploader.store!(photo)
@message[:photo] = my_uploader.filename
@message.save!
PhotoUploader:
def store_dir
Rails.env.production? ? (primary_folder = "production") : (primary_folder = "test")
"#{primary_folder}/media/#{model.id}"
end
Message 类:
class Message < ActiveRecord::Base
attr_accessible :content, :photo, :user_id
mount_uploader :photo, PhotoUploader
end
model.id 在存储路径中返回错误。即使保存后模型也是nil,文件存储在#{primary_folder}/media
I'm trying to write a method to store an image from a given url, inside a ruby worker. It comes along my Rails app in which I display the object image. Here is what I've come up with:
@message = Message.create!
my_uploader = PhotoUploader.new
photo = open(image_url)
@message[:photo] = my_uploader.store!(photo)
@message[:photo] = my_uploader.filename
@message.save!
the PhotoUploader:
def store_dir
Rails.env.production? ? (primary_folder = "production") : (primary_folder = "test")
"#{primary_folder}/media/#{model.id}"
end
the Message class:
class Message < ActiveRecord::Base
attr_accessible :content, :photo, :user_id
mount_uploader :photo, PhotoUploader
end
The model.id returns an error in the storage path. The model is nil even after saving it, and the file is stored in #{primary_folder}/media
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您没有从模型中调用
store!
,这就是model.id
不起作用的原因,因为model
是nil
这可能会为你做:
我在 Rails 控制台中尝试了这个,并且
@message[:photo].store!(photo)
给出了相同的错误,但@message.photo.store!(photo)
有效。I think it's that you're not calling
store!
from the model, which is whymodel.id
doesn't work, becausemodel
isnil
This will probably do it for you:
I tried this out in the rails console, and
@message[:photo].store!(photo)
gave the same error, but@message.photo.store!(photo)
worked.