Action name mistaken as show id

时间:2017-11-13 06:38:07

标签: ruby-on-rails

I'm still new to Rails, and so I have this issue.

I have a controller name Client. In the routing I have:

resources :clients do
   get 'confirmation_import/:page', action: :confirmation_import, on: :collection, :defaults => {:page => 1}
   collection do
     get :autocomplete
     post :confirmation_import
     post :import
   end
end

As you can see, I'm using pagination. When I go back to the first page though, the page param is not used, so the link becomes /clients/confirmation_import. From there I'm getting an error because a before_action for show action is being used, and it says my id param is confirmation_import. How do I fix this?

1 个答案:

答案 0 :(得分:1)

通常路由从上到下匹配,因为show操作的路由在confirmation_report操作的路由之前定义,它将show操作路由到confirmation_reportid

因此,请在confirmation_report路线之前设置show的路线,如下所示:

get 'clients/confirmation_import/:page' => 'clients#confirmation_import, :defaults => {:page => 1}

resources :clients do
  collection do
    get :autocomplete
    post :confirmation_import
    post :import
  end
end