在Rails App中上传/解析Excel文档

时间:2013-02-15 00:16:39

标签: ruby-on-rails ruby excel actioncontroller rubyxl

我正在创建一个应用程序,可以选择将单个条目添加到数据库模型,也可以从Excel文档中上载多个条目。我正在关注rails guides on uploading,希望有一种方法可以利用RubyXL gem上传Excel文档。以下是相关文档:

revenue_models / upload.html.erb

You are in the Upload view...
<%= form_for(@uploaded_doc, :url => {:action => parse_upload_revenue_models_path}, :html => {:method => "put"}) do |f| %>
    <%= f.file_field(:workbook) %>
    <%= f.submit("Upload File") %>                                                   
<% end %> 

的routes.rb

Dataway::Application.routes.draw do
    devise_for :users
    resources :revenue_models do
        get 'upload', :on => :collection         
    end
    match 'revenue_models/upload' => 'revenue_models#parse_upload', :via => :post
    root :to => "home#index"
end

revenue_models_controller.rb

...
def upload
    @uploaded_doc = { :workbook => RubyXL::Parser.new }
end
def parse_upload
    @worksheet = (params[:uploaded_doc])
    @worksheet_name = @worksheet.original_filename
end

revenue_models模型是最终将使用excel doc中的已解析数据进行修改的数据库。该模型不包含上载文档的列。我在upload.html.erb视图中将表单路由到parse_upload操作。但是,我收到以下错误:

NoMethodError in Revenue_models#upload
    undefined method `model_name' for NilClass:Class

该错误与upload.html.erb视图中的form_for有关。请帮忙。

0 个答案:

没有答案