代码之家  ›  专栏  ›  技术社区  ›  Kris

嵌套资源的语法形式是什么?

  •  2
  • Kris  · 技术社区  · 16 年前

    map.resources :websites do |website|
      website.resources :domains
    end
    

    <% form_for(@domain, :url => website_domains_path(@website)) do | form | %>
    <%= form.text_field :name %>
    
    # ArgumentError: wrong number of arguments (1 for 0)
    # form_helper.rb:290:in 'respond_to?'
    # form_helper.rb:290:in 'apply_form_for_options!'
    # form_helper.rb:277:in 'form_for'
    
    <% form_for([@website, @domain]) do | form | %>
    <%= form.text_field :name %>
    
    # ArgumentError: wrong number of arguments (1 for 0)
    # form_helper.rb:290:in 'respond_to?'
    # form_helper.rb:290:in 'apply_form_for_options!'
    # form_helper.rb:277:in 'form_for'
    
    <% form_for(:domain, @domain, :url => website_domains_path(@website)) do | form | %>
    <%= form.text_field :name %>
    
    # ArgumentError: wrong number of arguments (1 for 0)
    # wrapper.rb:14:in 'respond_to?'
    # wrapper.rb:14:in 'wrap'
    # active_record_helper.rb:174:in 'error_messages_for'
    
    <% form_for(:domain, [@website, @domain]) do | form | %>
    <%= form.text_field :name %>
    
    # UndefinedMethodError 'name' for #<Array:0x40fa498>
    

    我已经确认了 @website @domain 包含正确类的实例。

    路由也生成正确的是这样使用的例子,所以我不认为他们是一个问题的路由或网址助手。

    <%= website_domains_path(1) %>
    <%= website_data_source_path(1, 1) %>
    

    3 回复  |  直到 16 年前
        1
  •  1
  •   Hock    12 年前

    你可以用

    <%= form_for [@website, @domain] %>
    

    在哪里? @domain @website

        2
  •  1
  •   klew    16 年前

    我刚刚试过:

    <% form_for @note, :url => teams_person_notes_path(@person) do |f| %>
      <%= f.text_field :note %>
      <%= f.submit "Add" %>
    <% end %>
    

      map.namespace :teams do |t|
        t.resources :people do |p|
          p.resources :notes
        end
      end
    

    所以它与您的相同(只添加了名称空间,但不是case)。

    当然,您的示例表单是针对@domain的新对象的。它不适用于编辑操作,那么您应该:

     <% form_for @domain, :url => edit_website_domain_path(@website, @domain) do |form| %>
    
        3
  •  0
  •   Kris    16 年前

    这是如此明显,当它最终点击,我有一个字段称为 respond_to . 当然,它重写了一个现有的方法。