0

我有一些这样的路线:

resources :users do
  member do
    get 'services', :path => 'services/edit', :defaults => { :servicable => 'user' }
  end
end

这让我有localhost:3000/users/1/services/edit

我正在尝试为其添加动态路径。

我尝试添加:as => :edit_services_path

get 'services', :path => 'services/edit', :defaults => { :servicable => 'user' }, :as => :edit_services_path

所以我可以尝试这样的事情:

<%= link_to "Edit", edit_services_path %>

但这给了我错误。

undefined local variable or method `edit_services_path' for #<#<Class:0x007f856fd5a970>:0x007f856ff18690>

如果我正在自定义路径,我尝试搜索添加新路径的正确方法,但没有任何运气,

谢谢

4

1 回答 1

1

Using the method you specify does yield a dynamic path, just not the one you are trying to use. It yields :

services_user GET    /users/:id/services/edit(.:format)                users#services {:servicable=>"user"}

And adding the :as option creates:

edit_services_path_user GET    /users/:id/services/edit(.:format)                users#services {:servicable=>"user"}

Which is a bit confusing, since usually path is not specified in the path, but used as a helper on the route itself (i.e. services_user path would be services_user_path with the path helper) so if the naming maters a lot to you this can be finessed but it is generating the dynamic paths, you can use rake routes to view these as you change things as well.

于 2013-06-27T05:35:02.937 回答