Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 参数数量错误(给定0,应为4)_Ruby On Rails_Ruby_Csv_Model View Controller_Arguments - Fatal编程技术网

Ruby on rails 参数数量错误(给定0,应为4)

Ruby on rails 参数数量错误(给定0,应为4),ruby-on-rails,ruby,csv,model-view-controller,arguments,Ruby On Rails,Ruby,Csv,Model View Controller,Arguments,我得到这个设置错误。我的想法是,该文件无法正确访问csv。我正在尝试导入的。我必须从一个csv导入,以使用模型日期创建另一个csv。我应该在控制器和视图中放置什么来显示新的csv/操作数据?基本上,我如何将模型中的一个csv文件传递给manipulation orders.csv并输出到另一个csv文件redemption.csv模型中的代码只是告诉模型以某种方式计算orders.csv中的现有数字,以便在没有此参数错误的情况下导出 控制器我真的不知道该怎么办 class OrdersContr

我得到这个设置错误。我的想法是,该文件无法正确访问csv。我正在尝试导入的。我必须从一个csv导入,以使用模型日期创建另一个csv。我应该在控制器和视图中放置什么来显示新的csv/操作数据?基本上,我如何将模型中的一个csv文件传递给manipulation orders.csv并输出到另一个csv文件redemption.csv模型中的代码只是告诉模型以某种方式计算orders.csv中的现有数字,以便在没有此参数错误的情况下导出

控制器我真的不知道该怎么办

class OrdersController < ApplicationController
  def index
    orders = Order.new
  end

  def redemptions
    orders = Order.new
  end
end
该观点对此也没有信心

<h1>Chocolates</h1>
puts "#{order.purchased_chocolate_count}"
<%= link_to "CSV", orders_redemptions_path, :format => :csv %>
模型

require 'csv'

# Define an Order class to make it easier to store / calculate chocolate    tallies
class Order < ActiveRecord::Base

  module ChocolateTypes
    MILK      = 'milk'
    DARK      = 'dark'
    WHITE     = 'white'
    SUGARFREE = 'sugar free'
  end

  BonusChocolateTypes = {
    ChocolateTypes::MILK      => [ChocolateTypes::MILK, ChocolateTypes::SUGARFREE],
    ChocolateTypes::DARK      => [ChocolateTypes::DARK],
    ChocolateTypes::WHITE     => [ChocolateTypes::WHITE, ChocolateTypes::SUGARFREE],
    ChocolateTypes::SUGARFREE => [ChocolateTypes::SUGARFREE, ChocolateTypes::DARK]
  }

    # Ruby has this wacky thing called attr_reader that defines the available
    # operations that can be performed on class member variables from outside:
    attr_reader :order_value
    attr_reader :chocolate_price
    attr_reader :required_wrapper_count
    attr_reader :order_chocolate_type
    attr_reader :chocolate_counts

    def initialize(order_value, chocolate_price, required_wrapper_count, order_chocolate_type)
        @order_value            = order_value
        @chocolate_price        = chocolate_price
        @required_wrapper_count = required_wrapper_count
        @order_chocolate_type   = order_chocolate_type

        # Initialize a new hash to store the chocolate counts by chocolate type.
        # Set the default value for each chocolate type to 0
        @chocolate_counts = Hash.new(0);

        process
    end

    # Return the number of chocolates purchased
    def purchased_chocolate_count
        # In Ruby, division of two integer values returns an integer value,
        # so you don't have to floor the result explicitly
        order_value / chocolate_price
    end

    # Return the number of chocolate bonuses to award (which can include
    # multiple different chocolate types; see BonusChocolateTypes above)
    def bonus_chocolate_count
        (purchased_chocolate_count / required_wrapper_count).to_i
    end

    # Process the order:
    #   1. Add chocolate counts to the totals hash for the specified order type
    #   2. Add the bonus chocolate types awarded for this order
    def process
        chocolate_counts[order_chocolate_type] += purchased_chocolate_count

        bonus_chocolate_count.times do |i|
            BonusChocolateTypes[order_chocolate_type].each do |bonus_chocolate_type|
                chocolate_counts[bonus_chocolate_type] += 1
            end
        end
    end

    # Output the chocolate counts (including bonuses) for the order as an array
    # of strings suitable for piping to an output CSV
    def csv_data
        ChocolateTypes.constants.map do |output_chocolate_type|
            # Get the display string (lowercase)
            chocolate_key = ChocolateTypes.const_get(output_chocolate_type)
            chocolate_count = chocolate_counts[chocolate_key].to_i

            "#{chocolate_key} #{chocolate_count}"
        end
    end
  end


  # Create a file handle to the output file
  CSV.open("redemptions.csv", "wb") do |redemption_csv|

    # Read in the input file and store it as an array of lines
        input_lines = CSV.read("orders.csv")

    # Remove the first line from the input file (it just contains the CSV headers)
        input_lines.shift()

    input_lines.each do |input_line|
        order_value, chocolate_price, required_wrapper_count, chocolate_type = input_line

      # Correct the input values to the correct types
        order_value = order_value.to_f
        chocolate_price = chocolate_price.to_f
        required_wrapper_count = required_wrapper_count.to_i

        # Sanitize the chocolate type from the input line so that it doesn't
        # include any quotes or leading / trailing whitespace
        chocolate_type = chocolate_type.gsub(/[']/, '').strip

      order = Order.new(order_value, chocolate_price, required_wrapper_count, chocolate_type)
        order.process()

        puts order.purchased_chocolate_count

        # Append the order to the output file as a new CSV line
        output_csv << order.csv_data
    end
  end

在initialize方法中,不能为参数提供默认值

def initialize(order_value, chocolate_price, required_wrapper_count, order_chocolate_type)
当您试图运行orders=Order.new时,它需要四个参数,而您没有提供它

还有一个问题。对于正确的命名约定,局部变量名称应为order而不是orders

要正确指定默认值,可以查看