2012-09-09 64 views
0

當我有一個扁平的資源結構時,我的多層嵌套資源的新編輯頁面都工作正常。由於爲了構建更加合乎邏輯的結構而嵌套資源,這些頁面已經被打破了。 我有這開始,因爲這每個模型的單一表單模板:Rails - 在嵌套資源中新建/編輯頁面

<%= simple_form_for @contact, html: {:class => "well form-vertical"} do |f| %> 

這完全適用於非嵌套資源(如聯繫人,如上),允許創建和更新行動,以達到預期效果。

但是,對於嵌套資源(如Service,如下所示),新操作停止工作。當我瀏覽到「新」的頁面,我得到的錯誤:

Error 500: undefined method `services_path' for #<#<Class:0x0b3512b4>:0xb42b2c58> 

我對相關部門的routes.rb如下:

resources :contacts, shallow: true,  :except => [ :destroy ] do 
    resources :accounts, shallow: true,  :except => [ :destroy ] do 
    resources :services,     :except => [ :destroy ] 
    end 
end 

新和編輯聯繫人的控制器操作服務是:

聯繫人:

def new 
    @contact = Contact.new 
... 
def edit 
    @contact = Contact.find(params[:id]) 

服務:

def new 
    @service = Service.new(account_id: params[:account_id]) 
... 
def edit 
    @service = Service.find(params[:id]) 

的相關輸出rake routes是:

account_services  GET /accounts/:account_id/services(.:format)   services#index 
          POST /accounts/:account_id/services(.:format)   services#create 
new_account_service  GET /accounts/:account_id/services/new(.:format)  services#new 
     edit_service  GET /services/:id/edit(.:format)      services#edit 
      service  GET /services/:id(.:format)       services#show 
          PUT /services/:id(.:format)       services#update 
      contacts  GET /contacts(.:format)        contacts#index 
          POST /contacts(.:format)        contacts#create 
     new_contact  GET /contacts/new(.:format)       contacts#new 
     edit_contact  GET /contacts/:id/edit(.:format)      contacts#edit 
      contact  GET /contacts/:id(.:format)       contacts#show 
          PUT /contacts/:id(.:format)       contacts#update 
+0

正如我看到你真的沒有'services_path'幫手。只有'account_services_path'。 –

+0

我也看到了,但是如果在我的表單中添加:'url:account_services_path(account_id:params [:account_id])',那麼新動作將正常工作,但編輯停止工作,因爲編輯網址需要:id,而不是:帳戶ID。 – bdx

+0

我的主要關注點是讓事情變得簡單,我不想創建一個全新的頁面,因爲我必須更改一行,而其他行都是相同的。 – bdx

回答

0

事實證明,這是完全可以考慮到這個問題的錯誤的做法。我改寫了我的創建路線POST直接到/:controller,然後只使用simple_form_for @contact, html: {class: "well form-vertical"} do |f|

0

<%= simple_form_for(@contact, :html => {:class => "well form-vertical"}) do |f| %>使用,而不是

<%= simple_form_for @contact, html: {:class => "well form-vertical"} do |f| %> 
+0

當我像這樣添加括號時,出現'syntax error,unexpected',',期待')'' – bdx

+0

這個錯誤。 –

+0

<%= simple_form_for(@service,html:{:class =>「well form-vertical」})do | f | %> – bdx