未定义的方法`to_csv&#39;对于#<team:0x0000000c973f18>你的意思是? to_s

时间:2018-01-28 23:03:30

标签: ruby-on-rails csv export-to-csv ruby-on-rails-5

我正在尝试将数据库下载为 .csv 文件。转到 localhost:3000 / teams.csv 时出现此错误如何摆脱此错误? 这是我的团队数据库

class CreateTeams < ActiveRecord::Migration[5.1]
  def change
    create_table :teams do |t|
      t.integer :number
      t.integer :match
      t.string :scout
      t.boolean :start_with_cube_loaded
      t.boolean :cross_auto_line
      t.boolean :robot_cross_center_line
      t.boolean :did_they_place_cube_in_wrong_scale
      t.boolean :switch
      t.boolean :robot_hit_other_alliance_robot_in_null_zone_line
      t.boolean :did_they_mis_place_cube
      t.integer :dropped_cubes
      t.boolean :double_stack_scale
      t.boolean :did_they_foul
      t.integer :knocked_off_cubes
      t.integer :missed_cube
      t.boolean :climbed
      t.boolean :grab_field_bar
      t.boolean :climb_attemped
      t.boolean :lifted_two_robots
      t.boolean :provide_bar
      t.boolean :provide_ramp 
      t.boolean :grab_robot_bar
      t.boolean :climb_robot_ramp
      t.boolean :broke_died
      t.boolean :tipped_over
      t.timestamps
    end
  end
end

这是我的团队索引视图

<head> 
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>


  <div id="team">
    <h1> Team </h1>
    <%= form_with(model: @team) do |f| %>
        <div class="form-group">
        <%= f.number_field :number, id: "project-name-input", placeholder: "Team #", autoFocus: true %>
        <%= f.number_field :match, id: "project-name-input", placeholder: "Match #", autoFocus: true %>
        <%= f.text_field :scout, id: "project-name-input", placeholder: "Scout name", autoFocus: true %>
  </div>
  <div id="auto">
    <h1> Autonomous Play </h1>
    <p> Start with cube loaded<%= f.select :start_with_cube_loaded, [['Yes', true], ['No',false]] %></p>
    <p> Cross auto line<%= f.select :cross_auto_line, [['Yes', true], ['No',false]] %></p>
    <p> Robot cross center line <%= f.select :robot_cross_center_line, [['Yes', true], ['No',false]] %></p>
    <p> Did they place cube in wronge scale <%= f.select :did_they_place_cube_in_wrong_scale, [['Yes', true], ['No',false]] %> Switch <%= f.select :switch, [['Yes', true], ['No',false]] %></p>
    <p> Robot hit other alliance robot in null zone line <%= f.select :robot_hit_other_alliance_robot_in_null_zone_line, [['Yes', true], ['No',false]] %></p>
    <p> Did they mis-place cube <%= f.select :did_they_mis_place_cube, [['Yes', true], ['No',false]] %></p>


  </div>
  <div id="teleop">
  <h1> Teleop Play </h1>
        <p> Dropped cubes <%= f.number_field :dropped_cubes %></p>
        <p> Double stack scale <%= f.select :double_stack_scale, [['Yes', true], ['No',false]] %></p>
        <p> Did they foul <%= f.select :did_they_foul, [['Yes', true], ['No',false]] %></p>
        <p> Knocked off cubes <%= f.number_field :knocked_off_cubes %>
        <p> Missed Cubes <%= f.number_field :missed_cubes %>
  </div>
  <div id="post-game">
    <h1> Post game </h1>
    <p> Climbed <%= f.select :climbed, [['Yes', true], ['No',false]] %></p>
    <p> Grab field bar <%= f.select :grab_field_bar, [['Yes', true], ['No',false]] %></p>
    <p> Climb Attemped <%= f.select :climb_attemped, [['Yes', true], ['No',false]] %></p>
    <p> Lifted 2 Robots <%= f.select :lifted_two_robots, [['Yes', true], ['No',false]] %></p>
    <p> Provided Bar <%= f.select :provide_bar, [['Yes', true], ['No',false]] %></p>
    <p> Provide ramp <%= f.select :provide_ramp, [['Yes', true], ['No',false]] %></p>
    <p> Grab robot bar <%= f.select :grab_robot_bar, [['Yes', true], ['No',false]] %></p>
    <p> Climb robot ramp <%= f.select :climb_robot_ramp, [['Yes', true], ['No',false]] %></p>
    <p> Robot broke/died <%= f.select :broke_died, [['Yes', true], ['No',false]] %></p>
    <p> Tipped over <%= f.select :tipped_over, [['Yes', true], ['No',false]] %></p>
  </div>

<%= f.submit "Save", class: "btn btn-success" %>
<% end %>

这是我的团队控制器

class TeamsController < ApplicationController
  def index
    @team = Team.new
    respond_to do |format|
      format.html
      format.csv {send_data @team.to_csv}
    end
  end

  def create
    @team = Team.new(team_params)

    respond_to do |format|
      if @team.save
        format.js
        redirect_to '/teams.csv'
      else
        format.json { render json: @team.errors.messages, status: :unprocessable_entity }
      end
    end
  end

  private

  def team_params
    params.require(:team).permit(:number, :scout, :match,:start_with_cube_loaded,:cross_auto_line,:robot_cross_center_line,:did_they_place_cube_in_wrong_scale,:switch,:robot_hit_other_alliance_robot_in_null_zone_line,:did_they_mis_place_cube,:dropped_cubes,:double_stack_scale,:did_they_foul,:knocked_off_cubes,:missed_cube,:climbed,:grab_field_bar,:climb_attemped,:lifted_two_robots,:provide_bar,:provide_ramp,:grab_robot_bar,:climb_robot_ramp,:broke_died,:tipped_over )
  end

end

这是我的团队模型:team.rb:

class Team < ApplicationRecord
    def self.to_csv
        attributes = %w{number match scout start_with_cube_loaded cross_auto_line robot_cross_center_line did_they_place_cube_in_wrong_scale switch robot_hit_other_alliance_robot_in_null_zone_line did_they_mis_place_cube dropped_cubes double_stack_scale did_they_foul knocked_off_cubes missed_cube climbed grab_field_bar climb_attemped lifted_two_robots provide_bar provide_ramp grab_robot_bar climb_robot_ramp broke_died tipped_over}
        CSV.generate(headers: true) do |csv|
        csv << attributes

        all.each do |team|
          csv << team.attributes.values_at(*attributes)
        end
      end
    end
end

我的路线有以下资源:团队&#39;另外,我把要求&#39; csv&#39;在我的application.rb文件中。

1 个答案:

答案 0 :(得分:3)

您将to_csv定义为类方法,但您在实例上调用它。将@team.to_csv更改为Team.to_csv或向团队添加to_csv实例方法:

class Team < ApplicationRecord
  def to_csv
    # ...
  end
end