Ruby on rails 如何在RoR中解析上载文件的内容

Ruby on rails 如何在RoR中解析上载文件的内容,ruby-on-rails,ruby,Ruby On Rails,Ruby,我不熟悉Rails。 在我的项目中,用户必须上传文件,我将其存储 然后我必须解析文件内容并以新的形式显示它 我已经成功完成了文件上传部分, 现在,我应该如何读取它的内容呢?您可以使用该类在Ruby中打开文件并读取它们的内容,如下简单示例所示: # Open a file in read-only mode and print each line to the console file = File.open('afile.txt', 'r') do |f| f.each do |line|

我不熟悉Rails。 在我的项目中,用户必须上传文件,我将其存储 然后我必须解析文件内容并以新的形式显示它

我已经成功完成了文件上传部分,
现在,我应该如何读取它的内容呢?

您可以使用该类在Ruby中打开文件并读取它们的内容,如下简单示例所示:

# Open a file in read-only mode and print each line to the console
file = File.open('afile.txt', 'r') do |f|
  f.each do |line|
    puts line
  end
end

试着这样做:

upload = params[:your_upload_form_element]
content = upload.is_a?(StringIO) ? upload.read : File.read(upload.local_path)
非常小的文件可以作为字符串而不是上传的文件传递,因此您应该检查并相应地处理它。

完成示例 例如,上载包含联系人的导入文件。您不需要存储此导入文件,只需处理并丢弃它即可

路线 routes.rb

resources :contacts do 
  collection do
    get 'import/new', to: :new_import  # import_new_contacts_path

    post :import, on: :collection      # import_contacts_path
  end
end
<%= form_for @contacts, url: import_contacts_path, html: { multipart: true } do |f| %>

  <%= f.file_field :import_file %>

<% end %>
def new_import
end

def import
  begin
    Contact.import( params[:contacts][:import_file] ) 

    flash[:success] = "<strong>Contacts Imported!</strong>"

    redirect_to contacts_path

  rescue => exception 
    flash[:error] = "There was a problem importing that contacts file.<br>
      <strong>#{exception.message}</strong><br>"

    redirect_to import_new_contacts_path
  end
end
def import import_file 
  File.foreach( import_file.path ).with_index do |line, index| 

    # Process each line.

    # For any errors just raise an error with a message like this: 
    #   raise "There is a duplicate in row #{index + 1}."
    # And your controller will redirect the user and show a flash message.

  end
end
形式 查看/contacts/new_import.html.erb

resources :contacts do 
  collection do
    get 'import/new', to: :new_import  # import_new_contacts_path

    post :import, on: :collection      # import_contacts_path
  end
end
<%= form_for @contacts, url: import_contacts_path, html: { multipart: true } do |f| %>

  <%= f.file_field :import_file %>

<% end %>
def new_import
end

def import
  begin
    Contact.import( params[:contacts][:import_file] ) 

    flash[:success] = "<strong>Contacts Imported!</strong>"

    redirect_to contacts_path

  rescue => exception 
    flash[:error] = "There was a problem importing that contacts file.<br>
      <strong>#{exception.message}</strong><br>"

    redirect_to import_new_contacts_path
  end
end
def import import_file 
  File.foreach( import_file.path ).with_index do |line, index| 

    # Process each line.

    # For any errors just raise an error with a message like this: 
    #   raise "There is a duplicate in row #{index + 1}."
    # And your controller will redirect the user and show a flash message.

  end
end
希望能帮助别人


JP

当你说解析时,你在说什么格式?它是需要解析的html/xml/yaml文件还是纯文本文件?它是否有预定义的结构作为解析的起点?请确保在使用后关闭该文件,或者更好地使用
文件的块形式。打开
会自动关闭该文件。