我有一些模型在其他路径上共享相同的功能。所以我决定将这些方法放在一个模块中,并在模型中设置路径。我的问题是我无法访问模块中的属性。
模型:
class Job < ActiveRecord::Base
include ImageModel
image_dir = "jobs"
end
模块:
module ImageModel
extend ActiveSupport::Concern
def delete_image
unless pic_link == "" || pic_link == nil
begin
if File.delete(Rails.root.join("public", "images", image_dir, pic_link))
return true
else
return false
end
rescue
return true #an error occured but when the image does not exist we still return true
end
end
return true
end
def replace_image(new_image)
File.open(Rails.root.join("public", "images", image_dir, new_image.original_filename), "wb") do |f|
if f.write new_image.read
delete_image
pic_link = new_image.original_filename
return true #everything went fine
else
return false #return false if new image could not be written
end
end
end
end
我得到的错误:
undefined local variable or method `image_dir' for #<Job:0x007f8a93b9e8d8>
在这条线上:
File.open(Rails.root.join("public", "images", image_dir, new_image.original_filename), "wb") do |f|
我错过了什么还是我监督了一些重要的事情?
菲利克斯