Rails 4 - 控制器未被调用

时间:2017-01-18 10:13:22

标签: ruby-on-rails ruby-on-rails-4

我可以点击importers#import_vendor_ledger操作但似乎无法通过importers#import_chart_of_accounts中的redirect_based_on(arg)方法点击uploaders#upload_file操作。请帮忙!

注意:我遗漏了一些我认为没必要看的代码

我的代码:

的routes.rb

resources :uploaders do
   collection { post :upload_file }
end

resources :importers do
   collection { post :import_vendor_ledger, :import_chart_of_accounts }
end

index.html.haml

#chart_of_accounts
  = form_tag upload_file_uploaders_path, multipart: true do
    = hidden_field_tag :account, "chart_of_accounts"
    = file_field_tag :file
    = submit_tag 'Upload Chart of Accounts'

#vendor_ledger
  = form_tag upload_file_uploaders_path, multipart: true do
    = hidden_field_tag :account, "vendor_ledger"
    = file_field_tag :file
    = submit_tag 'Upload'

uploaders_controller.rb

class UploadersController < ApplicationController
  include Excel

  def upload_file
    uploaded_io = params[:file]
    if uploaded_io.path.downcase.end_with?(xlsx_extension)
      save_to_storage(uploaded_io)
      flash[:success] = 'File uploaded successfully!'
      redirect_based_on_(params) # this is where it should call the action
    else
      flash[:warning] = 'ERROR: The file you upload MUST be an ".xlsx" excel file!'
      redirect_to root_url
    end
  end

  private

  def redirect_based_on_(_params)
    case _params[:account]
    when "vender_ledger"
      redirect_to import_vendor_ledger_importers_path and return
    when "chart_of_accounts"
      redirect_to import_chart_of_accounts_importers_path and return
    end
  end
end

importers_controller.rb

class ImportersController < ApplicationController
  include Excel

  def index
  end

  def show
  end

  def import_vendor_ledger  # I can hit this action
    puts "hits vendor ledger import"
  end

  def import_chart_of_accounts  # but I can't hit this action
    puts "hits chart of accounts import"
  end

编辑#1:即使我在redirect_to import_chart_of_accounts_importers_path中明确致电uploaders#upload_file,它仍然没有点击importers#import_chart_of_accounts行动

编辑#2:在检查更多之后,似乎importers#import_chart_of_accounts动作被击中,但动作中没有任何函数被调用

1 个答案:

答案 0 :(得分:0)

将您的路线更改为以下内容:

resources :importers do
  collection do
    get :import_vendor_ledger
    get :import_chart_of_accounts
  end
end

编辑:因为你重定向到这两条路径,我相信,你需要那些GET路由。由于redirect_to会发出301 request,这将是GET请求。