我想从 Rails 内的模型文件中运行 Render 和 flash 等方法。我的代码是这样的:
class Product < ActiveRecord::Base
attr_accessible :name, :price, :released_on
validates :name, uniqueness: true
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |product|
csv << product.attributes.values_at(*column_names)
end
end
end
def self.import(file)
CSV.foreach(file.path , headers:true) do |row|
@product=Product.new(row.to_hash)
if @product.valid?
@product.save
flash[:notice] = "Product created!"
redirect_to(@product) and return
else
redirect_to action: :index
end
end
end
end
当我运行它并输入模型时,我收到 Flash 方法的错误。同样未定义渲染方法。任何猜测。