Ruby on rails 在Rails中CSV导入失败时显示flash
我的Rails应用程序中有一个有效的CSV导入功能 Item.rbRuby on rails 在Rails中CSV导入失败时显示flash,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我的Rails应用程序中有一个有效的CSV导入功能 Item.rb #CSV Import Function def self.import(file) CSV.foreach(file.path, headers: true) do |row| Item.create! row.to_hash end end def import current_user.items.import(params[:file]) flash[:success] = "
#CSV Import Function
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Item.create! row.to_hash
end
end
def import
current_user.items.import(params[:file])
flash[:success] = "Import Successful"
redirect_to spage3_path
end
def import
if current_user.items.import(params[:file])
flash[:success] = "Import Successful"
redirect_to spage3_path
else
flash[:danger] = "Error Encountered"
redirect_to spage3_path
end
end
项目\u controller.rb
#CSV Import Function
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Item.create! row.to_hash
end
end
def import
current_user.items.import(params[:file])
flash[:success] = "Import Successful"
redirect_to spage3_path
end
def import
if current_user.items.import(params[:file])
flash[:success] = "Import Successful"
redirect_to spage3_path
else
flash[:danger] = "Error Encountered"
redirect_to spage3_path
end
end
这对我来说很有效。但是,当导入失败时(我在项目模型的某些字段上进行了一些验证),应用程序就会崩溃。在这种情况下,我只想向用户显示一个flash[:danger]。为此,我修改了控制器,但现在每次我都会收到闪光[:危险]
项目\u controller.rb
#CSV Import Function
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Item.create! row.to_hash
end
end
def import
current_user.items.import(params[:file])
flash[:success] = "Import Successful"
redirect_to spage3_path
end
def import
if current_user.items.import(params[:file])
flash[:success] = "Import Successful"
redirect_to spage3_path
else
flash[:danger] = "Error Encountered"
redirect_to spage3_path
end
end
请告诉我哪里弄错了。谢谢
。创建代码>如果对象验证失败,则引发异常。您必须处理这种情况以避免应用程序崩溃。
保存对象之前,只需检查对象是否有效。
- 如果有效:保存它并继续解析CSV
- 如果无效:返回false并停止分析CSV
一个想法可以是:
项目1.rb
使用此方法,您可以保持控制器的现状
项目\u controller.rb
创建如果存在验证错误,代码>将引发异常。改用create
并检查返回值:
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
return :failed unless Item.create(row.to_hash)
end
end
并将控制器更改为:
def import
if current_user.items.import(params[:file]) == :failed
flash[:danger] = "Error Encountered"
else
flash[:success] = "Import Successful"
end
redirect_to spage3_path
end
不管实际的错误消息是什么,您的问题之一是,当您将import
设置为类方法时,您正在将其作为实例方法调用
两者的区别在于,类方法调用该类,而实例方法在已调用的类中运行:
#app/models/item.rb
class Item < ActiveRecord::Base
def self.import
#class method called by Item.import
end
def import
#instance method called by @item.import
end
end
这将使您能够调用:
def import
message = (@file = current_user.items.import(params[:file])) ? [:notice, "Import Successful"] : [:alert, @file]
redirect_to.send(spage3_path, "#{message[0]}: #{message[1]}")
end
--
这里有几点需要注意
首先,我使用了一个forimport
方法。我这么做是因为你这么叫它<代码>当前用户.items.import
坦白地说,我不知道你当前的设置是如何工作的——如果你调用current\u user.items.import
,我希望应用程序会返回一个错误。您正在对项
模型的集合调用import
,我希望该模型只能使用实例方法,而不是类
其次,您必须将异常返回给控制器。如果您没有收到任何异常消息,您希望如何调试?我的代码告诉您如何返回消息,尽管它可能不正确。我只是想知道这段代码首先是如何工作的。首先,在对象数组
上的项.rb
中调用导入类方法
,这是错误的,因为类方法
应该从类对象本身调用
,例如项中的导入方法
。rb
应该由其类项调用。导入
正是我的想法-您如何在类实例中使用类方法?错误消息是有用,我不知道我是否跟着你。我也有用户模型,与项目有很多关系。使用“Item.import(params[:file])”时,当前loggedin用户的用户id未分配给创建的项目,因此我将其更改为“current_user.items.import(params[:file]),因此,在从CSV成功创建项目时,用户id正在数据库中分配。”current#u user'helper函数返回登录的用户。对于CSV导入功能,我刚刚介绍了Railscasts#396Thanks@spickermann。成功验证时,在DB中创建项目,并显示flash[:success],但出现错误时,我看到的是flash[:success],而不是flash[:danger],并且项目未在DB中创建。有什么建议吗?因为它缺少上下文嘿@NickGnd它对我有用!在型号中使用了您的代码,并在我的控制器中检查了true/false以显示正确的闪存。并交叉检查数据库以验证项目创建。@RichPeck感谢您让我知道。我编辑了我的答案,以便更好地解释我的代码以及它是如何工作的。@Pushkar我很高兴能提供帮助:)没问题,谢谢你对它的成熟。我仍然认为答案缺乏背景,但如果OP成功了,那就取决于他:)谢谢@Rich Peck的详细解释。我也会尝试这种方法。Np,它可能需要做很多调整,但最终会得到比目前更有效的结果