我有一個問題,將圖像名稱存儲到數據庫中。圖片上傳到文件夾工作正常,但圖像的名字將不會保存到數據庫圖像名稱未插入數據庫在軌道上的紅寶石
型號代碼:
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
任何人指導我存檔這一個。提前致謝。
我發現下面張貼的解決方案。感謝您的支持。 – Sankar