我在将图像名称存储到数据库时遇到问题。图像上传到文件夹工作正常,但图像名称不会保存到数据库中
型号代码:
class Post < ActiveRecord::Base
attr_accessible :name, :imag
attr_accessor :imag
def self.save(upload)
name = upload['imag'].original_filename
directory = 'public/data'
# render :text => directory
# create the file path
path = File.join(directory,name)
# write the file
File.open(path, "wb") { |f| f.write(upload['imag'].read)}
end
end
控制器代码:
def create
@a=params[:post][:imag].original_filename /* how to pass in this image name into params[:post] */
pos= Post.save(params[:post])
if pos
redirect_to :action =>"index"
else
redirect_to :action =>"posts"
end
end
任何人都指导我存档这个。提前致谢。