CSVãµã¼ãã¼ããã¸ã§ã¯ãã®éå§
解決ããã課é¡
ä¼ç¤¾ã«ã¯æ次決ç®ãã¤ããã®ã ã決ç®ã§ã¯ãã¨ã¯ã»ã«ãªã©ã®è¡¨è¨ç®ã½ããã§å種è³æãä½æããã管çé¨éãä½æããè³æã¯ãé¨ã»èª²ã»ä¿ã»ååã³ã¼ãå¥çã
ã®çµç¹ãã¨ã«éè¨ããä¸è¦§è¡¨ã®å½¢å¼ã§ãããã¨ãå¤ããä½æããè³æã¯ãåé¨ç½²ã«é
å¸ããªããã°ãªããªããããããä»æã®ä¼ç¤¾ã§ããã以åã®ããã«ãå
¨ç¤¾ã®åè¨ãè¼ã£ãè³æãå
¨ã¦ã®äººã«é
å¸ããã¨ãä¸å±¤é¨ããã¯ã¬ã¼ã ãä»ããä¸å±¤é¨ããã¯ãåå½¹è·ã®æ
å½çµç¹ã®åã ãé
å¸ããããã«ã¨ã®æ示ããããããªãã®ã§ããã®æ示éãé
å¸ãããã¨ãããããããæå¤ã¨å¤§å¤ã§ãæ¯æã®å¤§ä»äºã«ãªã£ã¦ãã¾ãã
ã¾ããå
¨ç¤¾ã®çµç¹å¥ã®æ°åã表示ãããä¸è¦§è¡¨ãããé
å¸ããåä½ã®çµç¹ã®åã ãã³ãã¼ãã¦ãå¥ãã¡ã¤ã«ãä½æããããããåé¨ç½²ã®æ
å½è
å®ã«ã¡ã¼ã«ã®æ·»ä»ãã¡ã¤ã«ã¨ãã¦éä¿¡ãããæç« ã§æ¸ãã¨ããã ãã ãã以ä¸ã®ãããªå¤§å¤ããæ¯æãã£ã¨ç¶ãã®ã ...ã
- é å¸å ã®çµç¹ã20ããã°ã20ãã¡ã¤ã«ã«åããå¿ è¦ãããã
- å®å ã¨æ·»ä»ãããã¡ã¤ã«ãééããªãããã«ãååãªæ³¨æãå¿ è¦ã ã
- 人äºç°åã¯é »ç¹ã«ãããã¡ã¼ã«ã®å®å ã®ç®¡çã«ãæéããããã
- è³æã¯1ã¤ã ãã§ã¯ãªããé å¸ããè³æã®æ°ã ããä¸è¨ã®ä½æ¥è² æ ãå¢ãããã¨ã«ãªãã
- è³æã«è¨æ£ãããã°ãåéãããã¨ãããã
ããã§ãRailsã®åºçªã§ãããé å¸ããè³æãCSVãã¡ã¤ã«ã¨ãã¦æ¸ãåºãããããCSVãµã¼ãã¼ã«ã¢ãããã¼ããããåã¦ã¼ã¶ã¼ã¯ãã°ã¤ã³ããã¨ãèªåã®æ å½çµç¹ã®é¨åã ããå種è³æãé²è¦§ãããã¨ãåºæ¥ãããããªwebã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ãããcsv_serverããã¸ã§ã¯ãã®å§ã¾ããæããã¦æãéãã®ãã®ãåºæ¥ããã©ãã...ã
-
-
- å¤ãã®ä¼ç¤¾ã§ã¯ãä¸è¨ã®ãããªè³æé å¸ããã©ã®ããã«å¯¾å¿ãã¦ããã®ã ãããï¼è¦å´æå°éã®æ¹æ³ãç¥ããã...ãããã¨ã大ããè¦å´ãããªãï¼
-
CSVãã¡ã¤ã«ãã¢ãããã¼ããã¦ãé²è¦§ããã
ååã¾ã§ãCSVãã¡ã¤ã«ãåãæ±ãã¯ã©ã¹ãä½æãã¦ã試è¡é¯èª¤ãã¦ã¿ããããã¯ãRailsã§éçºãããªãããã¼ã¿ãã¼ã¹ã«åãè¾¼ãã§ãã¾ã£ãæ¹ãè¯ãã¨æããããã®æ¹ããæéãããããªãããé«æ©è½ãªActiveRecordããããããªãã¨ãå©ãã¦ãããã大ã¾ããªå¦çã®æµãã¯ä»¥ä¸ã®ããã«ããäºå®ãã¾ã ãã¦ã¼ã¶ã¼ãã¨ã®é²è¦§å¶éã¯ãªãã
CSVãã¡ã¤ã«ã管çããCSVã¢ãã«ããã¥ã¼ãã³ã³ããã¼ã©ã¼
ã¢ãã«ã®ä½æ
csvã¢ãã«ã«ã¯ã以ä¸ã®æ©è½ã追å ããäºå®ã
- ã¢ãããã¼ããããã¡ã¤ã«ãå ã«ããã¼ã¿ãã¼ã¹ã«ãã¡ã¤ã«åã¨åããã¼ãã«ã追å ããã
- 追å ãããã¼ãã«ã«ãCSVãã¡ã¤ã«ã®å 容ãã¤ã³ãã¼ãããã
script/generate model csv
csvsãã¼ãã«ãä½æ
- ãã¤ã°ã¬ã¼ã
- db/migrate/001_create_csvs.rb
class CreateCsvs < ActiveRecord::Migration def self.up create_table :csvs do |t| t.column :file_name, :string t.column :file_comment, :string t.column :file_size, :integer t.column :created_at, :datetime t.column :updated_at, :datetime end end def self.down drop_table :csvs end end
scaffoldã®å®è¡
script/generate scaffold csv
csvã¢ãã«ãã³ã¼ãã£ã³ã°
- ã¢ãã«
- app/models/csv.rb
- CSVãã¡ã¤ã«ãã¢ãããã¼ãããæã«ããã¼ãã«ã追å ãã¦ããã¼ã¿ãã¼ã¹ã«ã¤ã³ãã¼ãããå¦çãè¡ãã
class Csv < ActiveRecord::Base # ãã¡ã¤ã«åããã¼ãã«åã«ãªãã®ã§ãéè¤ãã§ãã¯ãè¡ãã validates_uniqueness_of :file_name # ã¢ãããã¼ããããã¡ã¤ã«ãæå®ããªãã¨ãã¨ã©ã¼ã«ããã def validate errors.add(:file_name, "ã¢ãããã¼ããããã¡ã¤ã«ãæå®ãã¦ãã ããã") if @file.nil? || @file.size == 0 end # Tempfileãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ä¿åããã # new()ããupdate_attributes()ã®æãå¼ã³åºãããã def tempfile=(file) return if file.nil? || file.size == 0 @file = file self.file_name = file.original_filename self.file_size = file.size # file.read.sizeã¨ãã£ã¦ãã¾ãã¨ã29è¡ç®ã§@file.readããæããªããnilãè¿ãã # 1åã¢ã¯ã»ã¹ããã¨ããã®ãã¼ã¿ã¯æ¶ãã¦ãã¾ãï¼ end # ãã¼ãã«ã追å ããã def create_csv_table execute_sql("create table #{table_name} (#{csv_columns} PRIMARY KEY (id));") end # ãã¡ã¤ã«åã®æ¡å¼µåã®æåã¾ã§ããã¼ãã«åã¨ãã¦è¿ãã def table_name File.basename(self.file_name, ".*") end # CSVãã¡ã¤ã«ã®1è¡ç®ããã£ã¼ã«ãåã¨ãã¦è¿ãã # å¦çã®éç¨ã§ã以ä¸3ã¤ã®å¦çãåæã«å®è¡ããã # - æåã³ã¼ãã®å¤æãä½åãªç©ºç½ãç¹æ®æåãæ´å½¢ããå¦çã # - idãã£ã¼ã«ãã®è¿½å å¦çã # - ãã¼ã¿é¨åãpublic/temp.csvãã¡ã¤ã«ã¨ãã¦ä¿åããå¦çã def csv_columns # ã"ããåãé¤ãã¦ãæåã³ã¼ããUTF-8ã«å¤æãã¦ãæ¹è¡ã§åºåã£ãé åãè¿ãã str = @file.read.gsub(/"+/, '') str = NKF.nkf('-w', str) lines = str.split("\n") # é åã®å é ãåãåºãã¦ãã«ã³ãã§åºåã£ãé åã«ãã¦ãä½åãªç©ºç½ãåé¤ããã columns = lines.shift.split(",").collect{|n| n.strip} # idåãåå¨ããªããã°è¿½å ããã if columns.index("id").nil? columns.unshift("id") id = 0 lines.collect! {|line| id +=1; "#{id},#{line}"} end # ã¤ã³ãã¼ãç¨ã®ãã¡ã¤ã«ãä¿åããã File.delete("public/temp.csv") rescue nil File.open("public/temp.csv", "wb") do |f| f.write(lines.join("\n")) end # ãã£ã¼ã«ãå string, ... ã®æ¸å¼ã§è¿ãã columns.inject("") do |result, column| result << "#{column} string, " end end # csvãã¼ã¿ãã¤ã³ãã¼ãããã # ã¤ã³ãã¼ããé«éåãããããsqlite3ã®ã¤ã³ãã¼ãå½ä»¤ããç´æ¥ã³ãã³ãå®è¡ããã def import_csv env = ENV['RAILS_ENV'] || 'development' system("sqlite3", "-separator", ",", "db/#{env}.sqlite3", ".import public/temp.csv #{table_name}") end # ãã¼ãã«ãåé¤ããã def drop_csv_table execute_sql("drop table #{table_name}") end # ãã¼ãã«ã®è¿½å ãåé¤ã®SQLãå®è¡ããã def execute_sql(sql) env = ENV['RAILS_ENV'] || 'development' db = SQLite3::Database.new("db/#{env}.sqlite3") db.execute(sql) db.close end end
csvsã³ã³ããã¼ã©ã¼ã®ä¿®æ£
- ã³ã³ããã¼ã©ã¼
- app/controllers/csvs_controller.rb
class CsvsController < ApplicationController ...ï¼éä¸çç¥ï¼... def create @csv = Csv.new(params[:csv]) if @csv.save @csv.create_csv_table @csv.import_csv flash[:notice] = 'Csv was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end ...ï¼éä¸çç¥ï¼... def destroy @csv = Csv.find(params[:id]).destroy @csv.drop_csv_table redirect_to :action => 'list' end ...ï¼éä¸çç¥ï¼... end
-
-
- ãªã¬ã³ã¸è²ã®é¨åã追è¨ããã
-
csvãã¥ã¼ã®ä¿®æ£
- æ°è¦ã¢ãããã¼ã
- app/views/csvs/new.rhtml
- start_form_tagã«ã:multipart => trueã追å ããããã¡ã¤ã«ãã¢ãããã¼ãããããã®ãã¾ããªããå¿ããã¨ã¢ãããã¼ãã§ããªãã
<h1>New csv</h1> <%= start_form_tag({:action => 'create'}, :multipart => true) %> <%= render :partial => 'form' %> <%= submit_tag "Create" %> <%= end_form_tag %> <%= link_to 'Back', :action => 'list' %>
- å ¥åãã©ã¼ã
- app/views/csvs/_form.rhtml
<%= error_messages_for 'csv' %> <!--[form:csv]--> <p><label for="csv_tempfile">File</label><br/> <%= file_field 'csv', 'tempfile' %></p> <p><label for="csv_file_comment">File comment</label><br/> <%= text_field 'csv', 'file_comment' %></p> <!--[eoform:csv]-->
- ã¢ãããã¼ãããCSVãã¡ã¤ã«ã®ãªã¹ã表示
- app/views/csvs/list.rhtml
<h1>Listing csvs</h1> <table> <tr> <% for column in Csv.content_columns %> <th><%= column.human_name %></th> <% end %> </tr> <%= render :partial => 'listd', :collection => @csvs %> </table> <%= link_to 'Previous page', { :page => @csv_pages.current.previous } if @csv_pages.current.previous %> <%= link_to 'Next page', { :page => @csv_pages.current.next } if @csv_pages.current.next %> <br /> <%= link_to 'New csv', :action => 'new' %>
- ãªã¹ã表示ã®ãã¼ã¿é¨åãæç»
- app/views/csvs/_listd.rhtml
- ãã¡ã¤ã«åã®ãªã³ã¯ã¯ãdisplaysã³ã³ããã¼ã©ã¼ã®listã¡ã½ãããå¼ã³åºãã¦ããã
- ãã®æããã©ã¡ã¼ã¿ã¼ã¨ãã¦ãã¼ãã«åãéä¿¡ãã¦ããã
- displaysã³ã³ããã¼ã©ã¼ã§ã¯ããã®ãã¼ãã«åãè¦ã¦ãåç §ãããã¼ãã«ãåãæ¿ããããã«ããã
- "12345678".gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\1,')ãå®è¡ããã¨ã"12,345,678"ãè¿ã£ã¦ããã
- ã¤ã¾ããæååããæ°åãçºè¦ãã¦ã3æ¡åºåãã§ã«ã³ããå ¥ãã¦ãããæ£è¦è¡¨ç¾ã ãï¼ã¡ããã¨ç解ã§ãã¦ããªãã...ãï¼
- åèãã¼ã¸ï¼Rubyのある風景 - Regexp Lookahead
<!--[:]--> <tr> <td><%= link_to h(listd.file_name), :controller => 'displays', :action=>'list', :table => listd.table_name %></td> <td><%=h listd.file_comment %></td> <td></td> <td align="right"><%=h listd.file_size.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\1,') %> B</td> <td><%=h listd.created_at.strftime('%Y-%m-%d %H:%M:%S') %></td> <td><%=h listd.updated_at.strftime('%Y-%m-%d %H:%M:%S') %></td> <td align="center"><%= link_to 'Show', :action => 'show', :id => listd %></td> <td align="center"><%= link_to 'Edit', :action => 'edit', :id => listd %></td> <td align="center"><%= link_to 'Destroy', { :action => 'destroy', :id => listd }, :confirm => 'Are you sure?', :post => true %></td> </tr> <!--[:]-->
ã¢ãããã¼ããããå 容ãé²è¦§ããDisplayã¢ãã«ããã¥ã¼ãã³ã³ããã¼ã©ã¼
ã¢ãã«ã®ä½æ
displayã¢ãã«ã¯ã«ã以ä¸ã®æ©è½ã追å ããäºå®ã
- ã¢ãããã¼ããããå 容ãä¿æãã¦ãããã¼ãã«ããajax_scaffoldããã¼ã¹ã«åç §ããã
- Displayã¢ãã«1ã¤ã§ãè¤æ°ã®ãã¼ãã«ãåãæ¿ãã¦ãå 容ã表示ããã
script/generate model display
displaysãã¼ãã«ãä½æ
- ãã¤ã°ã¬ã¼ã
- db/migrate/002_create_displays.rb
- ãããã追å ããããã¼ãã«ãåç §ããã®ã§ãããã§è¿½å ãããã¼ãã«ã¯ä¸è¦ã ãããã®å¾ã®scaffoldãå®è¡ããããã便å®çã«è¿½å ãã¦ãããã
class CreateDisplays < ActiveRecord::Migration def self.up create_table :displays do |t| t.column :name, :string end end def self.down drop_table :displays end end
ajax_scaffoldã®å®è¡
- ã¾ã ãajax_scaffold_generatorãã¤ã³ã¹ãã¼ã«ãã¦ããªãå ´åã¯ã以ä¸ãå®è¡ãã¦ããã
gem install ajax_scaffold_generator
- ãã¨ã¯é常ã®scaffoldã¨åãããã«å®è¡ããã
script/generate ajax_scaffold display
displaysã³ã³ããã¼ã©ã¼ã®ä¿®æ£
- ã³ã³ããã¼ã©ã¼
- app/controllers/displays_controller.rb
- åç §ãããã¼ãã«ãåãæ¿ããå¦çã追å ããã
- 1ãã¼ã¸ã®è¡¨ç¤ºä»¶æ°ã50件ã«ããã
class DisplaysController < ApplicationController include AjaxScaffold::Controller # å ¨ã¦ã®å¦çã«å ç«ã£ã¦ãselect_tableã¡ã½ããã§åç §ãããã¼ãã«ãé¸æããã before_filter :select_table after_filter :clear_flashes before_filter :update_params_filter # 1ãã¼ã¸ã®è¡¨ç¤ºä»¶æ°ãè¨å® def default_per_page 50 end # 渡ãããtableãã©ã¡ã¼ã¿ã¼ãããã¼ãã«åã¨ãã¦ã»ããããã°ãè¤æ°ã®ãã¼ãã«ãåãæ¿ãã¦åç §ã§ããã def select_table Display.set_table_name params[:table] || 'displays' end ...ï¼ä»¥ä¸çç¥ï¼...
-
-
- ãªã¬ã³ã¸è²ã®é¨åã追è¨ããã
-
以ä¸ã§ãCSVãã¡ã¤ã«ãã¢ãããã¼ããã¦ããã®å
容ã表示ã§ããããã«ãªã£ããã¾ã ãajax_scaffoldå´ã®æ©è½ã¯å®å
¨ã«å©ç¨ã§ããªãããä»å¾ä¿®æ£ãã¦ããäºå®ã