這是我的代碼。營救活動記錄:記錄無效
class Product < ActiveRecord::Base
attr_accessible :name, :price, :released_on
begin
validates :name, uniqueness: true
rescue ActiveRecord::RecordInvalid => e
render(inline: "RESCUED ActiveRecord::RecordInvalid")
return
end
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.create! row.to_hash # If we want to add a new item
end
end
end
當我保存有相同名稱的重複模式將引發異常
的ActiveRecord :: RecordInvalid中的ProductsController#進口
Validation failed: Name has already been taken
Rails.root: /home/deepender/396-importing-csv-and-excel/store-before
我使用的救援行動還在它是不是處理錯誤?任何猜測,我錯了。
其實我的控制器沒有創建方法,基本上在這個應用程序,我導入CSV文件,並創建一個數據庫。請參閱上面的更新代碼。 –