简体   繁体   English

Rails 4-控制器未被调用

[英]Rails 4 - controller not being called

I can hit the importers#import_vendor_ledger action but I can't seem to hit the importers#import_chart_of_accounts action via the redirect_based_on(arg) method in uploaders#upload_file . 我可以执行importers#import_vendor_ledger操作,但似乎无法通过uploaders#upload_fileredirect_based_on(arg)方法执行importers#import_chart_of_accounts操作。 Please help! 请帮忙!

NOTE: I left out some code that I didn't think was necessary to see 注意:我遗漏了一些我认为不必要的代码

My code: 我的代码:

routes.rb 的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 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 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 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

EDIT #1: even if I explicitly call redirect_to import_chart_of_accounts_importers_path in uploaders#upload_file it still doesn't hit the importers#import_chart_of_accounts action 编辑#1:即使我在上uploaders#upload_file redirect_to import_chart_of_accounts_importers_path中显式调用redirect_to import_chart_of_accounts_importers_path ,它仍然不会影响importers#import_chart_of_accounts操作

EDIT #2: after inspecting more, it seems that importers#import_chart_of_accounts action IS being hit, but none of the functions in the action is being called 编辑#2:经过更多检查后,似乎importers#import_chart_of_accounts动作被击中,但该动作中的任何功能都没有被调用

Change your route to something like this: 将您的路线更改为以下内容:

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

EDIT: Since you are redirecting to those two paths, I believe, you need those to be GET routes. 编辑:由于您将重定向到这两个路径,我相信,您需要将它们作为GET路由。 Since redirect_to issues a 301 request which will be GET request. 由于redirect_to发出了301 request ,该301 request将成为GET请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM