2011-11-04 6 views
0

csv 파일에서 레일 3에 자동 완성을 수행 할 방법이 있는지 알고 싶습니다. 나는 컨트롤러와 함께 HTML 부분 (나는 그것에 대해 모르겠다) 렌더링을하려고 해요. 코드를 아래에 게시. 내가 레일에 새로운 해요, 당신의 도움이 많이레일 3 CSV 파일에서 jquery 자동 완성

class CsvreaderController < ApplicationController 
    def search_csv 
    unless params[:search_no].nil? 
     @winner_attrs = nil 

     @search_no = params[:search_no] 
     @file_path = params[:file_path] 

     File.open(@file_path, "r") do |infile| 
     p "file opened in read mode" 

     while (line = infile.gets) 
      attrs = line.split("\t") 

      if attrs[0].to_i.equal? @search_no.to_i 
      p "match found" 
      p @winner_attrs = attrs 

      respond_with do |format| 
       format.html do 
       if request.xhr? 
        render :partial => "search_csv", 
         :locals => { :result => @winner_attrs }, 
         :layout => false, 
         :status => :created 
       else 
        redirect_to "search_csv" 
       end 
       end 
      end 

      end 
     end 
     end 

    end 
    end 
end 

보기 파일을 감사합니다 :

<script> 
$('#csv_form').bind('ajax:success', function(evt, data, status, xhr) { 
    $('#result').html(xhr.responseText); 
}); 
</script> 

<%= form_tag({ :action => 'search_csv' }, 
       :remote => 'true', 
       :id  => 'csv_form' 
    ) do 
%> 
    <%= text_field_tag 'search_no', nil, :class => 'textbox' %> 

    <%= hidden_field_tag 'file_path', 'public/data/final_draw_till_10_Dec.csv' %> 

    <%= submit_tag "Submit", :name => 'button', :class =>'button' %> 
<% end %> 

<div id="result"></div> 

답변

1

여기에 가장 큰 붉은 깃발은 열고 CSV는 모든 요청에 ​​파일을 구문 분석하고 있다는 점이다. 이것은 값 비싼 작업이며 확실히 가치가 없습니다. 자동 완성은 기본적으로 검색 작업이며 CSV 파일은 검색을 위해 설계되지 않았습니다. CSV 파일을 생성하거나 변경 한 후에 만 ​​구문 분석 한 다음 검색을 위해 설계된 구조 (예 : 색인이 좋은 데이터베이스)에 데이터를 저장해야합니다 (

).