I am trying to download the database as a .csv file. I get this error when going to localhost:3000/teams.csv How to I get rid of this error? This is my teams database
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
this is my teams index view
<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 %>
this is my teams controller
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
This is my teams model: 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
and my routes has 'resources :teams' , also, I put require 'csv' in my application.rb file.
You defined to_csv
as a class method but you're calling it on an instance. Either change @team.to_csv
to Team.to_csv
or add a to_csv
instance method to Team:
class Team < ApplicationRecord
def to_csv
# ...
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.