1

これは、csv ファイルの列を検証するために使用したい RoR コードです。ただし、「rowdata」配列の値は検証メソッドに渡されません。どんな助けでも大歓迎です。

#Ruby on Rails code
def validate_rows
    count_row=0
    #@passed_file is a csv file which is read from a submitted html form.
    @parsed_file=CSV::Reader.parse(params[:dump][:csvfile])
    @parsed_file.each  do |row|
        # Array of columns to be validated
        validate_cols = [:admission_no, :class_roll_no]
        rowdata={'admission_no'=>row[0],'class_roll_no'=>row[1]}


        #validate colums by sending value from the array (rowdata)  to a method injected
        #by the following code. However value from rowdata array is not passed
        #to the method.
        if count_row >=1
            valid = validate_cols.inject(true){|valid_sum, col|
                valid_sum && send("validate_#{col}", rowdata[col])
            }
            #if validation fails return row number.
            if not (valid)
                return count_row
            end
        end
        count_row=count_row+1
    end 
    #if validation suceeds return 0
    return 0
end

#The following methods are called by the inject funnction (valid)
def validate_admission_no(passed_value)
    if(passed_value)
        return true
        else
        return false
    end
end

def validate_class_roll_no(passed_value)
    if(passed_value)
        return true
        else
        return false
    end
end
4

1 に答える 1

1

私の提案は、記号の代わりに文字列を使用することです

 def validate_rows
count_row=0
#@passed_file is a csv file which is read from a submitted html form.
@parsed_file=CSV::Reader.parse(params[:dump][:csvfile])
@parsed_file.each  do |row|
    # Array of columns to be validated
    validate_cols = ["admission_no", "class_roll_no"]
    rowdata={'admission_no'=>row[0],'class_roll_no'=>row[1]}


    #validate colums by sending value from the array (rowdata)  to a method injected
    #by the following code. However value from rowdata array is not passed
    #to the method.
    if count_row >=1
        valid = validate_cols.inject(true){|valid_sum, col|
            valid_sum && send("validate_#{col}", rowdata["#{col}"])
        }
        #if validation fails return row number.
        if not (valid)
            return count_row
        end
    end
    count_row=count_row+1
end 
#if validation suceeds return 0
return 0

終わり

于 2012-10-31T06:26:46.780 に答える