Railsを初めて使用し、データベースから結果をエクスポートする方法を理解しましたが、CSVファイルから新しいレコードを作成する際に問題が発生します。以下のコードを使用して、CSVファイルをインポートし、最後の2列にユーザーからのセッションデータを入力できるようにします。今のところ、これを機能させるために静的な番号を挿入しました。現在、エラーメッセージとして「ActionDispatch :: Http::UploadedFileを文字列に変換できません」と表示されます
CSVドキュメント
name、task、expected_results、require_id Test Name 1、Open a File、Open、t Test Name 2、Read a File、Read、t Test Name 3、Close a File、Close、f
コントローラ
def csv_import
file = params[:file]
FasterCSV.foreach(file,{:headers => true, :row_sep => :auto}) do |row|
Script.create!(:name => row[0],
:task => row[1],
:expected_results => row[2],
:require_id => row[3],
:department_id => 1,
:category_id => 1)
end
end
見る
<%=form_tag '/script_admin/csv_import', :multipart => true do%>
<%= file_field_tag "file" %><br/>
<%= submit_tag("Import") %>
<% end %>
DB移行
class CreateScripts < ActiveRecord::Migration
def self.up
create_table :scripts do |t|
t.integer :department_id, :null => false
t.integer :category_id, :null => false
t.string :name, :null => false
t.string :task, :null => false
t.string :expected_results, :null => false
t.boolean :require_id, :null => false, :default => "t"
t.timestamps
end
end
def self.down
drop_table :scripts
エンドエンド
これに関する助けはありがたいです
〜カイル