Ruby on rails Rails中条件导入CSV的语法
以下导入方法需要处理设置为nil的输入变量Ruby on rails Rails中条件导入CSV的语法,ruby-on-rails,ruby,Ruby On Rails,Ruby,以下导入方法需要处理设置为nil的输入变量 def self.import(file, unit_id, profit_center_column [...] CSV.foreach(file.path, :col_sep => "\t", headers: true, skip_blanks: true) do |row| begin Asset.create( unit_id: unit_id, profit_center_id:
def self.import(file, unit_id, profit_center_column [...]
CSV.foreach(file.path, :col_sep => "\t", headers: true, skip_blanks: true) do |row|
begin
Asset.create(
unit_id: unit_id,
profit_center_id: row[profit_center_column] unless profit_center_column.nil?,
[...]
其中启动此方法的控制器定义
params[:profit_center_column] = @unit.profit_center_column
其中@unit.price\u center\u column=nil
我遇到了一个意外的语法错误,意外的修饰符\u除非,期望'”
在使用除非之前,我从未使用过括号,我希望在这里使用逗号可以清楚地分隔语句。语法哪里搞砸了
Rails 4.2.4和Ruby 2.3.4正在使用中
注意,我也尝试了if语句,但这导致加载随机的xxx_列信息。您正在向资产的构造函数传递一个条件。如果您需要处理它,我建议首先使用确保的not nil值初始化资产
,然后对其余属性使用setter:
asset = Asset.new(
unit_id: unit_id,
....
asset.profit_center_id = row[profit_center_column] unless profit_center_column.nil?
asset.save
是的<代码>创建
是新建
加上保存
。。。我的注意力集中在树上,而不是森林!实际上,条件应该是asset.profit\u center\u id=[…]