Я хотел бы знать, есть ли способ сделать автозаполнение в rails 3 из файла CSV.Я пытаюсь сделать это с контроллером визуализации HTML частично (я не уверен в этом). Размещение кода ниже.я новичок в рельсах, ваша помощь очень ценится
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>