繁体   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