【问题标题】:Rails 4 routes with simple_form and shallow nested resources带有 simple_form 和浅层嵌套资源的 Rails 4 路由
【发布时间】:2014-12-10 00:30:39
【问题描述】:
  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,但您似乎需要同时为该方法提供 @textnote@account 对象才能使其工作?
  • 您声明了textnote_path 而不是textnotes_path。这可能是问题吗? @textnote 对象是什么?

标签: ruby-on-rails ruby-on-rails-4 simple-form nested-resources


【解决方案1】:

是否需要在simple_form_for 中添加url 参数,指定accounts_textnotes_path

通过浅层嵌套设置,您将发布到父资源的索引路径(我相信上面的路由输出显示了这一点)。

如果这不起作用,您可以随时尝试指定字符串路径或操作。

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

【讨论】:

    【解决方案2】:

    你很亲密。您必须像这样指定父帐户:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      相关资源
      最近更新 更多