Вопрос

I have a model called package, which has a controller package created under a name space admin.

so my resource in my routes is declared as below

namespace :admin do
    resources :package
end

when I run rake routes

admin_package_index GET  /admin/package(.:format) admin/package#index
                    POST   /admin/package(.:format) admin/package#create
new_admin_package GET    /admin/package/new(.:format) admin/package#new
edit_admin_package GET    /admin/package/:id/edit(.:format) admin/package#edit
admin_package GET    /admin/package/:id(.:format)  admin/package#show
              PUT    /admin/package/:id(.:format)  admin/package#update
              DELETE /admin/package/:id(.:format)  admin/package#destroy

If you see there is no helper method generated for create, which should have been admin_packages_path

controller

@newpackage = Package.new

view.html.erb

form_for [:admin,@newpackage] do |f|
end

is reporting that it is not able to find the admin_packages_path. Can somebody please explain how should we declare this in routes to generate the proper helper method for create ?

Это было полезно?

Решение

Your routes are named incorrectly, you're pairing a plural (resources) with a singular (:package).

If you will be working with multiple packages, you should declare resources :packages - this will generate you correctly named routes for all seven RESTful actions (index, show, new, create, edit, update, destroy).

If you are only working with a single package, you will need to specify the URL as an option for your form manually, eg.

form_for [:admin, @newpackage], url: admin_package_path do |f|

Другие советы

You really need to changes the routes to following:

namespace :admin do
    resources :packages
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top