2016-05-31 52 views
1

我已經命名空間的控制器實體::客戶模塊路由用的form_for(@object)

class Entities::CustomersController < ApplicationController 
... 
end 

和命名空間的ActiveRecord模型:

class Entities::Customer < Entities::User 

end 

在我的routes.rb文件我有:

resources :customers, module: :entities 

該模塊:實體存在,因爲我不想有如下路線:

/實體/客戶但僅限於:

/客戶

的問題,當我使我的形式開始:

<%= simple_form_for(@customer) do |f| %> 
     <%= f.input :email %> 
     <%= f.input :password %> 
     <%= f.input :name %> 
     <%= f.button :submit %> 
<% end %> 

這將引發錯誤:未定義的方法`entities_customer_path」類..

因此錯誤是軌認爲,正確的路徑是與前綴實體。

耙路線給我:

   Prefix Verb URI Pattern     Controller#Action 
     customers GET /customers(.:format)   entities/customers#index 
       POST /customers(.:format)   entities/customers#create 
    new_customer GET /customers/new(.:format)  entities/customers#new 
    edit_customer GET /customers/:id/edit(.:format) entities/customers#edit 
     customer GET /customers/:id(.:format)  entities/customers#show 
       PATCH /customers/:id(.:format)  entities/customers#update 
       PUT /customers/:id(.:format)  entities/customers#update 
       DELETE /customers/:id(.:format)  entities/customers#destroy 
+0

爲客戶提供請你的路由 –

+0

資源:客戶,模塊:實體如在描述 –

+0

看到我的意思'耙的輸出routes' –

回答

2

好了,所以有些掙扎後,我已經找到了解決這個問題:

的simple_form_for(@model)生成與實體前綴的路由,因爲它不不知道路線上有範圍的路徑。

所以在我的_form部分我不得不手動告訴導軌使用哪個路線取決於action_name輔助方法在我的部分。

<% 
case action_name 
    when 'new', 'create' 
    action = send("customers_path") 
    method = :post 
    when 'edit', 'update' 
    action = send("customer_path", @customer) 
    method = :put 
end 
%> 

<%= simple_form_for(@customer, url: action, method: method) do |f| %> 
     <%= f.input :email %> 
     <%= f.input :password %> 
     <%= f.input :name %> 
     <%= f.button :submit %> 
<% end %>