選択したファイルを決定ボタンでpostした際に、別タブで選択されたcsvファイルの内容を表示したいのですが、tableで出力する前に、ファイルの内容が出力されてしまいます。どうしたら良いのでしょうか?
test_controller.rb
controller
1def index 2end 3 4def import 5 # 選択ファイルの取得 6 upload_file = params[:file] 7 # temfileのpathを取得 8 file = upload_file.path 9 #CSV.tableで読み込む 10 @table = CSV.table(file) 11end
index.html.erb
view
1<%= form_tag import_filters_path, multipart: true do %> 2 <%= file_field_tag :file %> 3 <%= submit_tag "CSVをインポート" ,class: "btn btn-info", formtarget: "_blank" %> 4<% end %> 5
import.html.erb
view
1<h1>filletrcontroller#import</h1> 2<table> 3 <thead> 4 <tr> 5 <th>a</th> 6 <th>b</th> 7 <th>c</th> 8 </tr> 9 </thead> 10 11 <tbody> 12 <%= @table.each do |row| %> 13 <tr> 14 <td><%= row[:a] %></td> 15 <td><%= row[:b] %></td> 16 <td><%= row[:c] %></td> 17 </tr> 18 <% end %> 19 </tbody> 20</table>
routes.rb
route
1resources 'filters' do 2 collection { post :import } 3 collection { post :confirm } 4end 5
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/11 05:51 編集