Rails routes issue with controller testing

倾然丶 夕夏残阳落幕 提交于 2019-12-11 06:17:36

问题


I'm at a loss as to what I'm doing wrong here. Anyone have any insight?

Here's my routes.rb

resources :accounts do
    collection do
        get "search/:term/:offset/:limit.:format", :action => "search", :constraints => { :offset => /\d+/, :limit => /\d+/ }
    end
end

Here's my rake routes output...

GET    /accounts/search/:term/:offset/:limit.:format    {:offset=>/\d+/, :action=>"search", :controller=>"accounts", :limit=>/\d+/}

Here's my test line...

get :search, :term => "Test", :offset => 0, :limit => 2

Here's my error...

ActionController::RoutingError: No route matches {:term=>"Test", :action=>"search", :controller=>"accounts", :offset=>0, :limit=>2}

Any ideas?

Thanks in advance!


回答1:


I found the issues...

1) It is expecting to match on strings so instead of

:offset => 0, :limit => 2

it should be

:offset => '0', :limit => '2'

2) :format was not optional. I chose to make it an optional param, but if you encounter this you will have to pass format along if you don't make it optional.



来源:https://stackoverflow.com/questions/7434764/rails-routes-issue-with-controller-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!