简体   繁体   中英

Rails 4 routes with simple_form and shallow nested resources

  resources :accounts, shallow: true do
    resources :textnotes 
  end

Gives me

   account_textnotes GET    /accounts/:account_id/textnotes(.:format)     textnotes#index
                     POST   /accounts/:account_id/textnotes(.:format)     textnotes#create
new_account_textnote GET    /accounts/:account_id/textnotes/new(.:format) textnotes#new
       edit_textnote GET    /textnotes/:id/edit(.:format)                 textnotes#edit
            textnote GET    /textnotes/:id(.:format)                      textnotes#show
                     PATCH  /textnotes/:id(.:format)                      textnotes#update
                     PUT    /textnotes/:id(.:format)                      textnotes#update

When I try to create a new note

http://0.0.0.0:3000/accounts/4/textnotes/new
<%= simple_form_for(@textnote) do |f| %>

I get the following error:

NameError in Textnotes#new
Showing /Users/xyz/rails_projects/crm/app/views/textnotes/_form.html.erb where line #1 raised:

undefined method `textnotes_path' for #<#<Class:0x007f8204f37360>:0x007f8204be4fa0>

Do you need to add a url parameter to simple_form_for specifying accounts_textnotes_path ?

With the shallow nesting setup you're posting to the index path of the parent resource (which I believe your routes output above shows).

If that doesn't work you could always try specifying a string path or an action.

(As always, http://edgeguides.rubyonrails.org/form_helpers.html and http://guides.rubyonrails.org/routing.html may help.)

You were close. You have to specify the parent account like this:

<%= simple_form_for([@account, @textnote]) do |f| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM