0
我想知道是否有任何方法可以從csv文件在rails 3中自動完成。我試圖做到這一點與控制器呈現HTML部分(我不知道這一點).Posting下面的代碼。我是新的軌道,你的幫助是非常讚賞rails 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>