从当前模型的视图中引用另一种模型的方法

时间:2016-03-12 13:36:59

标签: ruby-on-rails csv

我对Rails很陌生。

我正在开发一个应用程序,您可以在其中上传csv文件或创建手动银行交易。以前我在一个名为Transaction的表中有手动输入和上传的csv文件条目。现在,我尝试将上传的文件条目存储在名为Bank的单独表中。 所以我有一个银行模型和一个交易模型及其各自的控制器和视图。

之前我曾经有过" New Transaction"表格和"上传CSV"表格在同一页面中称为"新交易"避免为两者创建单独的视图,它工作正常。 现在,在我为CSV上传条目创建了一个新模型(Bank)之后,我尝试从我的" New Transaction"中引用银行模型的导入方法路径。页面导入csv文件。但它给我一个负载错误。

LoadError in BanksController#new
Unable to autoload constant BanksController, expected /app/controllers/banks_controller.rb to define it

这是我的银行模式:

class Bank < ActiveRecord::Base

    belongs_to :admin
    paginates_per 10

    def self.to_csv(options = {})
      CSV.generate(options) do |csv|
        csv << column_names
        all.each do |transaction|
          csv << transaction.attributes.values_at(*column_names)
        end
      end
    end

    extend ActiveModel::Model
    include ActiveModel::Validations
    include ActiveModel::Conversion
    attr_accessor :file

    validates :date, presence: true
    validates :notes, presence: true
    validates :t_type, presence: true
    validates :t_method, presence: true
    validates :amount, presence: true
    validates :paid_by, presence: true
    validates :paid_to, presence: true
    #validates :cashbox, presence: true
    validates :bank, presence: true

    def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
            transaction_hash = row.to_hash
            t = Bank.new(transaction_hash.except("cashbox"))
            t.id = Bank.maximum(:id).next
            t.save!
        end
    end    
end

这是我的银行控制员:

class BankController < ApplicationController
    before_filter :authenticate_admin!

    def index
        @banks = Bank.all.order('created_at DESC')
        respond_to do |format|
            format.html
            format.csv { send_data @banks.to_csv }
        end
    end

    def import
        begin
            Bank.import(params[:file])
            redirect_to root_url, notice: "Transactions Imported!"
        rescue ActiveRecord::RecordInvalid => exception
            redirect_to :back, alert: "#{exception.message}"
        end
    end

    def editview
        @q = Transaction.ransack(params[:q])
        respond_to do |format|
            format.html
            format.csv { send_data @transactions_list_for_csv.to_csv, filename: 'transactions.csv' }
        end
    end
end

这是我在交易模型中的新交易视图,其中包含新交易和上传csv的表格:

<div class="col-md-6">
    <div class="container new-container">
        <h3>New Transaction</h3>
        <%= render 'form' %> </br>
        <%= link_to "Back", root_path %>
    </div>
</div>

<div class="divider"></div>

<div class="col-md-6">
    <div class="import-col">
        <h3>Import a CSV File:</h3>
        <%#= form_tag import_transactions_path, multipart: true do %>
        <%= form_tag import_banks_path, multipart: true do %>
            <%= file_field_tag :file, class: "form-control", required: true %></br>
            <%= submit_tag "Import CSV", class: "btn btn-success" %>
        <% end %>
    </div>
</div>

这是Transactions控制器的新方法:

def new 
        @transaction = Transaction.new
    end 

1 个答案:

答案 0 :(得分:1)

控制器应该以复数形式命名。将其重命名为 Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/usr/lib64/R/library/ROracle/libs/ROracle.so': libclntsh.so.11.1: cannot open shared object file: No such file or directory Error: package 'OREdm' could not be loaded