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

通过scaffold生成的视图中的坏函数调用是什么?

  •  0
  • Greg  · 技术社区  · 16 年前

    我已经把东西搭好了:

    script/generate scaffold wip/thing name:string
    

    在视图中得到一些无效的函数调用,如:

    <td><%= link_to 'Edit', edit_thing_path(thing) %></td>
    

    引发此错误的原因:

    ActionView::TemplateError (undefined method `edit_thing_path' for #<ActionView::Base:0xb5c00944>) on line #11 of app/views/wip/things/index.html.erb:                                                                                                                                 
    8:   <tr>                                                                                                                                  
    9:     <td><%=h thing.name %></td>
    10:     <td><%= link_to 'Show', thing %></td>
    11:     <td><%= link_to 'Edit', edit_thing_path(thing) %></td>
    12:     <td><%= link_to 'Destroy', thing, :confirm => 'Are you sure?', :method => :delete %></td>
    13:   </tr>
    14: <% end %>
    

    我在具有命名空间的路由中定义了资源:

      map.namespace :wip do |wip|
        wip.resources :things
      end
    

    rake routes告诉我:

                                    wip_things GET    /wip/things(.:format)                                                            {:action=>"index", :controller=>"wip/things"}
                                               POST   /wip/things(.:format)                                                            {:action=>"create", :controller=>"wip/things"}
                                 new_wip_thing GET    /wip/things/new(.:format)                                                        {:action=>"new", :controller=>"wip/things"}
                                edit_wip_thing GET    /wip/things/:id/edit(.:format)                                                   {:action=>"edit", :controller=>"wip/things"}
                                     wip_thing GET    /wip/things/:id(.:format)  
    

    我假设这些名称(wip\u thing,new\u wip\u thing)是正确的名称,但它仍然给我这个错误

    谢谢。

    2 回复  |  直到 16 年前
        1
  •  2
  •   shingara    16 年前

    此方法来自routes.rb文件。如果您有一个资源:thing define,那么所有这些方法都在您的控制器/视图中定义。

    config/routes.rb 如果您是:

    map.resources :things

    如果您没有此资源,则此方法不是define。

    http://guides.rubyonrails.org/routing.html

    您可以通过rake任务了解所有这些路由:

    rake routes

        2
  •  0
  •   Greg    16 年前

    知道了!方法应符合

    rake routes
    

    但应具有后缀路径:

    <%= link_to 'Edit', edit_wip_thing_path(@thing) %>
    
    推荐文章