无法加载此类文件 - map_fields

时间:2016-07-07 09:37:12

标签: ruby-on-rails

在我的Rails 3.2.13应用程序中,有一个导入联系页面,我们可以通过csv导入联系人,如果缺少任何字段或映射,我们可以手动映射标题。为此,他们使用了map_fields插件,其中大部分代码都是公开的 - >插件文件夹。 现在,当我尝试在4.2.6应用程序中运行相同时,这是我面临的错误:

  

LoadError - 无法加载此类文件 - map_fields:activesupport   (4.2.6)lib / active_support / dependencies.rb:274:在block in require'
activesupport (4.2.6) lib/active_support/dependencies.rb:238:in
块中   在load_dependency'activesupport(4.2.6)中   lib / active_support / dependencies.rb:647:in new_constants_in'
activesupport (4.2.6) lib/active_support/dependencies.rb:238:in
load_dependency'activesupport(4.2.6)   lib / active_support / dependencies.rb:274:在require'
app/controllers/contacts_controller.rb:2:in
'中   app / controllers / contacts_controller.rb:1:在<top (required)>'
activesupport (4.2.6) lib/active_support/dependencies.rb:457:in
块中   在load_file'activesupport(4.2.6)中   lib / active_support / dependencies.rb:647:in new_constants_in'
activesupport (4.2.6) lib/active_support/dependencies.rb:456:in
load_file'activesupport(4.2.6)   lib / active_support / dependencies.rb:354:in require_or_load'
activesupport (4.2.6) lib/active_support/dependencies.rb:494:in
load_missing_constant'activesupport(4.2.6)   lib / active_support / dependencies.rb:184:在`const_missing'

这是我的控制者:

class ContactsController < ApplicationController
  require 'map_fields'
  map_fields :mapper, ['First Name','Last Name', 'Email', 'Notes'], :file_field => :file, :params => [:contact]

  def create
    @user = User.find(params[:user_id])
    @contact = @user.contacts.create(contact_params)
    respond_to do |format|
      if @contact.save
        format.json { render :json => { :notice => 'Contact was successfully created!',:redirect => user_contacts_url} }
        format.html { redirect_to(user_contacts_url(@user), :notice => 'Contact was successfully created!', :type => 'success') }
       else
        format.json { render :json => {:redirect => false} }
        format.html { render :action => "edit" }
      end
    end
  end
  def import
    @contact = Contact.new 
  end
    def mapper
    @contact = Contact.new(params[:contact])
    count = 0

    if fields_mapped?
      mapped_fields.each do |row|
        params[:contact] = {"user_id" => @current_user.id, "firstname" => row[0], "lastname" => row[1], "notes" => row[3], "email" => row[2].try(:strip)}
        contact = Contact.new(params[:contact])

        if contact.save
          count = count + 1
        end
      end

      if count > 0
        flash[:notice] = "#{count} Contact(s) created"
        redirect_to :action => :index
      else 
        flash[:notice] = "No contact was created..." 
        redirect_to :action => :index
      end

    else
      @best_row = @rows[1]
      render
    end

    rescue MapFields::InconsistentStateError
      flash[:error] = 'Please try again'
      redirect_to :action => :import
    rescue MapFields::MissingFileContentsError
      flash[:error] = 'Please upload a file'
      redirect_to :action => :import
  end

end

除此之外,我有联系人/ import.html.erb:

<%= semantic_form_for :contact, :url => mapper_user_contacts_path do |f| %>
   <%= f.inputs :name => 'Please select a CSV file to upload' do %>
     <%= f.input :file, :label => false, :as => :file, :input_html => {:name => "file", :id => "file"} %>
   <% end %>
   <%= f.buttons do %>
     <%= f.commit_button raw('Import contacts') %>
   <% end %> 
 <% end %>

和app / views / contacts / mapper.html.erb:

<%= render :partial => 'map_fields/map_fields' %>

以上呈现字段位于供应商中 - &gt;插件 - &gt; map_fields文件夹。

请帮助我如何克服错误?是否从插件文件夹中呈现数据?

0 个答案:

没有答案