Tengo un espacio de nombres en mis rutas.rb
namespace :businesses do
resources :registration
end
Mi controlador está en un subdirectorio negocios / registration_controller.
def new
@business = Business.new
end
En mi opinión, quiero hacer esto form_for @business do |f| ...
pero recibo el siguiente error:
No route matches {:controller=>"businesses", :action=>"create"}
Reinicié el servidor y también obtengo esto:
undefined method
business_path 'para # <#: 0x10339bb20> `
Aquí están mis rutas de rastrillo:
home_index GET /home/index(.:format) {:action=>"index", :controller=>"home"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
user_password PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
user PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
user DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
full_tree_admin_categories GET /admin/categories/full_tree(.:format) {:action=>"full_tree", :controller=>"admin/categories"}
admin_categories GET /admin/categories(.:format) {:action=>"index", :controller=>"admin/categories"}
admin_categories POST /admin/categories(.:format) {:action=>"create", :controller=>"admin/categories"}
new_admin_category GET /admin/categories/new(.:format) {:action=>"new", :controller=>"admin/categories"}
edit_admin_category GET /admin/categories/:id/edit(.:format) {:action=>"edit", :controller=>"admin/categories"}
admin_category GET /admin/categories/:id(.:format) {:action=>"show", :controller=>"admin/categories"}
admin_category PUT /admin/categories/:id(.:format) {:action=>"update", :controller=>"admin/categories"}
admin_category DELETE /admin/categories/:id(.:format) {:action=>"destroy", :controller=>"admin/categories"}
businesses_registration_index GET /businesses/registration(.:format) {:action=>"index", :controller=>"businesses/registration"}
businesses_registration_index POST /businesses/registration(.:format) {:action=>"create", :controller=>"businesses/registration"}
new_businesses_registration GET /businesses/registration/new(.:format) {:action=>"new", :controller=>"businesses/registration"}
edit_businesses_registration GET /businesses/registration/:id/edit(.:format) {:action=>"edit", :controller=>"businesses/registration"}
businesses_registration GET /businesses/registration/:id(.:format) {:action=>"show", :controller=>"businesses/registration"}
businesses_registration PUT /businesses/registration/:id(.:format) {:action=>"update", :controller=>"businesses/registration"}
businesses_registration DELETE /businesses/registration/:id(.:format) {:action=>"destroy", :controller=>"businesses/registration"}
root /(.:format) {:action=>"index", :controller=>"home"}
ruby-on-rails
Dex
fuente
fuente
Respuestas:
Si tiene rutas con espacios de nombres, la mejor manera es:
class Admin::BusinessesController < ApplicationController def new @business = Business.new end end
en rutas.rb:
namespace :admin do resources :businesses end
En vista:
form_for [:admin, @business] do |f|...
Los documentos: http://guides.rubyonrails.org/form_helpers.html sección 2.3.1 Tratar con espacios de nombres
Respecto a su caso:
En route.rb todo está bien. En la vista, debe escribir la URL explícitamente porque tiene una variable en el controlador que no sea el nombre del controlador:
form_for @business, :url => business_registration_path do |f|...
Supongo que en empresas / registration_controller tienes algo como esto:
class Businesses::RegistrationController < ApplicationController def new @business = Business.new end end
Y un comentario: no crearía registration_controller para registrar una nueva empresa. Creo que mantener las acciones relacionadas con el negocio en business_controller es mucho más claro.
fuente
undefined method
business_businesses_path 'para # <# <Class: 0x103385d70>: 0x103384088> `redirect_to admin_business_path(@business)
o tal vezredirect_to [:admin, @business]
?De hecho, creo que hay una mejor solución.
form_for [:admin, @business]
el problema de dar una URL es que si abstrae el formulario como una vista parcial, tendrá que lidiar con situaciones de "crear" y "actualizar". Apunta a diferentes URL y termina proporcionando el
@url
controlador in.fuente