Ruby on rails 未定义的方法`to#csv';对于#<;团队:0x0000000c973f18>;你是说?到

Ruby on rails 未定义的方法`to#csv';对于#<;团队:0x0000000c973f18>;你是说?到,ruby-on-rails,csv,export-to-csv,ruby-on-rails-5,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.bo

我正在尝试以.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
class CreateTeams
这是我的团队索引视图

<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 %>

团队
自主游戏
从加载多维数据集开始

交叉自动线

机器人十字中心线

他们把立方体放错刻度开关了吗

机器人在零区域线击中其他联盟机器人

他们放错地方了吗

电视剧 丢弃的立方体

双层秤

他们犯规了吗

敲碎的方块 遗漏的立方体 赛后 攀登

抓杆

攀登计划

举起2个机器人

提供酒吧

提供坡道

抓斗机器人杆

爬升机器人坡道

机器人破产/死亡

翻倒

这是我的团队管理员

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
class teamscocontroller
这是我的团队模型: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
class团队csv您将
to_csv
定义为类方法,但您正在对实例调用它。请将
@team.to_csv
更改为
team.to_csv
或将
to_csv
实例方法添加到团队:

class Team < ApplicationRecord
  def to_csv
    # ...
  end
end
class团队
您将
to_csv
定义为类方法,但您正在实例上调用它。请将
@team.to_csv
更改为
team.to_csv
或将
to_csv
实例方法添加到团队:

class Team < ApplicationRecord
  def to_csv
    # ...
  end
end
class团队
谢谢@abevoelker,这真的很有帮助Hanks@abevoelker,真的很有帮助