通过脚手架生成的视图中的函数调用错误是怎么回事?

发布于 2024-08-27 12:47:35 字数 2158 浏览 8 评论 0原文

我已经搭建了 Things 元素:

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 路由给了我这个:

                                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_thing,new_wip_thing)是正确的名称,但它仍然给我这个错误,

谢谢。

I've scaffolded Things element:

script/generate scaffold wip/thing name:string

and got some invalid function call in views, like:

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

Which raise this error:

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 %>

What's with that function? Where is it? Is it some kind of automagic stuff or do I need to implement it (if so - where should it go?)

I have resource defined in routes with namespace:

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

rake routes gives me this:

                                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)  

I assumed that those names (wip_thing, new_wip_thing) are the correct names, but it's still gives me that error

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他夏了夏天 2024-09-03 12:47:35

该方法来自您的routes.rb 文件。如果您有资源:事物定义,则所有此方法都在您的控制器/视图中定义。

检查您的 config/routes.rb 文件,如果您是:

map.resources :things

如果您没有此资源,则此方法未定义。

检查有关 Ruby on Rails 指南的资源:http://guides.rubyonrails.org/routing.html< /a>

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

rake paths

This method come from your routes.rb file. If you have a resource :thing define, all of this method are define in your controller/views.

Check on your config/routes.rbfile if you are :

map.resources :things

If you don't have this resource, this methode is not define.

Check about this resource on Ruby on Rails guides : http://guides.rubyonrails.org/routing.html

You can know all of this routes with the rake task :

rake routes

往日 2024-09-03 12:47:35

知道了!该方法应按照建议,

rake routes

但应具有后缀 _path:

<%= link_to 'Edit', edit_wip_thing_path(@thing) %>

got it! The method should be as suggested by

rake routes

but should have suffix _path:

<%= link_to 'Edit', edit_wip_thing_path(@thing) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文