Вопрос

My new and edit pages for multiple levels of nested resources all worked fine when I had a flat resource structure. Since nesting resources for the purpose of making a more logical structure, these pages have been a bit broken. I have a single form template for each model which starts as this:

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

This works perfectly for the non-nested resources (such as Contact, as above), allowing create and update actions to work as expected.

With the nested resources however (such as Service, as below), the new action stops working. When I browse to the 'new' page, I get the error:

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

My routes.rb for the relevant section is as follows:

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

The controller actions for new and edit for contacts and services are:

Contact:

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

Service:

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

The relevant output from rake routes is:

   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
Это было полезно?

Решение 2

As it turns out, this was entirely the wrong approach to be taking to this problem. I instead rewrote my create routes to POST straight to /:controller and it then worked with just using simple_form_for @contact, html: {class: "well form-vertical"} do |f|

Другие советы

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

<%= simple_form_for @contact, html: {:class => "well form-vertical"} do |f| %>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top