Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Routes Namespaces and form_for

I have namespace in my routes.rb

  namespace :businesses do     resources :registration   end 

My controller is in a subdirectory businesses/registration_controller.

def new   @business = Business.new end 

In my view, I want to do this form_for @business do |f| ... but I am getting the following error:

No route matches {:controller=>"businesses", :action=>"create"}

Restarted the server and I'm also getting this:

undefined methodbusinesses_path' for #<#:0x10339bb20>`

Here are my rake routes:

                   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"} 
like image 523
Dex Avatar asked Oct 04 '10 08:10

Dex


People also ask

What is namespace in Rails routes?

This is the simple option. When you use namespace , it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How do Rails routes work?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

What are Rails resource routes?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.


2 Answers

If you have namespaced routes the best way is:

class Admin::BusinessesController < ApplicationController   def new     @business = Business.new   end end 

in routes.rb:

namespace :admin do   resources :businesses end 

In view:

form_for [:admin, @business] do |f|... 

The Docs: http://guides.rubyonrails.org/form_helpers.html section 2.3.1 Dealing with Namespaces

Regarding your case:

In routes.rb everything is o'k. In the view you should write url explicitly because you have variable in controller other than controller name:

form_for @business, :url => business_registration_path do |f|... 

I suppose that in businesses/registration_controller you have something like this:

class Businesses::RegistrationController < ApplicationController   def new     @business = Business.new   end end 

And one remark: I wouldn't create registration_controller for registering a new business. I think that keeping business related actions in business_controller is much clearer.

like image 80
Voldy Avatar answered Sep 20 '22 08:09

Voldy


Actually, I think there is a better solution.

form_for [:admin, @business] 

the issue with giving a url is that if you abstract the form out as a partial view, you'll need to deal with "create" and "update" situations. They points to different urls, and ends up with providing the @url in controller.

like image 30
fengd Avatar answered Sep 23 '22 08:09

fengd