2010-10-04 72 views
76

我在routes.rb中有命名空間Rails的路線命名空間和的form_for

namespace :businesses do 
    resources :registration 
    end 

我的控制器是一個子目錄企業/ registration_controller。

def new 
    @business = Business.new 
end 

在我看來,我想這樣做form_for @business do |f| ...,但我收到以下錯誤:

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

重新啓動服務器,我也得到這個:

undefined method business_path'爲#<#:0x10339bb20>'

這裏是我的耙路線:

    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"} 
+0

你能發佈「耙路線」輸出嗎? – Bohdan 2010-10-04 08:15:45

回答

160

如果你已經命名空間的路線最好的辦法是:

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

在routes.rb中:

namespace :admin do 
    resources :businesses 
end 

鑑於:

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

文檔:http://guides.rubyonrails.org/form_helpers.html第2.3.1節與命名空間

關於你的情況進行處理:

在routes.rb中一切都是o'k。在視圖中,你應該寫地址明確,因爲你比控制器名稱其它控制器具有可變:

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

我想,在企業/ registration_controller你有這樣的事情:

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

而且一個備註:我不會創建registration_controller來註冊一個新的業務。我認爲在business_controller中保持業務相關的行爲要清晰得多。

+0

是的,這就是我對控制器的要求。我實際上按照你的建議嘗試了,並且我爲#<#獲得了未定義的方法'business_businesses_path':0x103384088>' – Dex 2010-10-04 08:55:47

+0

我同意!但是,我將需要Admin類子部分的這種類型的功能。也許它會在下一次工作。 – Dex 2010-10-04 09:02:35

+0

回答更新:) – Voldy 2010-10-04 09:07:50

22

其實我覺得還有更好的解決辦法。

form_for [:admin, @business] 

與給人一種網址的問題是,如果你抽象的形式作爲一個局部視圖,你需要處理「創建」和「更新」的情況。他們指向不同的網址,最後在控制器中提供@url

+0

這已經是Voldy解決方案的一部分。最後一行。 – Dex 2012-12-26 22:46:40

+0

@Dex,即redirect_to – fengd 2012-12-27 15:29:45