2016-05-30 27 views
1

我正在使用Rails 4.2.3。我有這個在我的config/routes.rb文件即使我已經在我的config/routes.rb文件中定義了該方法,但卻得到了「The action」show'找不到'

resources :my_objects do 
    get "import" 
end 

,並在我的應用程序/控制器/ my_objects_controller.rb文件

def import 
    puts "starting" 
    service = XACTEService.new(「Stuff」, '2015-06-01', 'Zoo') 
    service.process_my_object_data 
    puts "finished" 
end 

已經定義了這個當我訪問http://localhost:3000/my_objects/import,我得到這個錯誤:

The action 'show' could not be found for MyObjectsController」 error.

我在日誌文件中看不到來自我的import操作的「puts」語句。我還需要做些什麼才能調用import方法?

回答

0

import路由名稱正在被視爲show的參數,因爲路由未正確定義。檢查rake routes的輸出,看看它在這種情況下實際做了什麼。

相反,你需要定義這樣的路線:

resources :my_objects do 
    collection do 
    get "import" 
    end 
end 

或像這樣:

resources :my_objects do 
    member do 
    get "import" 
    end 
end 

Rails Routing from the Outside In引導有一節叫Adding More RESTful Actions,這將有助於你確定這些選項適合您的應用程序。

相關問題