簡體   English   中英

具有simple_form和淺層嵌套資源的Rails 4路線

[英]Rails 4 routes with simple_form and shallow nested resources

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

給我

   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

當我嘗試創建新筆記時

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

我收到以下錯誤:

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>

您是否需要在simple_form_for添加一個url參數以指定accounts_textnotes_path

使用淺層嵌套設置時,您將發布到父資源的索引路徑(我相信上面的路由輸出顯示了該路徑)。

如果這樣不起作用,您可以隨時嘗試指定字符串路徑或操作。

(一如既往, http://edgeguides.rubyonrails.org/form_helpers.htmlhttp://guides.rubyonrails.org/routing.html可能會有所幫助。)

你近了 您必須像這樣指定上級帳戶:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM