对路线进行别名会导致 Rails 预期路径不存在

发布于 2024-09-01 09:09:45 字数 4088 浏览 2 评论 0原文

好的,这里有一些代码:

prompt>rails my_app
prompt>cd my_app
prompt>script/generate scaffold service_type title:string time_allotment:integer
prompt>rake db:migrate

然后编辑这些文件,如下所示:

#routes.rb:
ActionController::Routing::Routes.draw do |map|
  map.resources :services, :controller => :service_types
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

生成这些路由:

prompt>rake routes
    services GET    /services(.:format)                {:controller=>"service_types", :action=>"index"}
             POST   /services(.:format)                {:controller=>"service_types", :action=>"create"}
 new_service GET    /services/new(.:format)            {:controller=>"service_types", :action=>"new"}
edit_service GET    /services/:id/edit(.:format)       {:controller=>"service_types", :action=>"edit"}
     service GET    /services/:id(.:format)            {:controller=>"service_types", :action=>"show"}
             PUT    /services/:id(.:format)            {:controller=>"service_types", :action=>"update"}
             DELETE /services/:id(.:format)            {:controller=>"service_types", :action=>"destroy"}
                    /:controller/:action/:id           
                    /:controller/:action/:id(.:format)

_

 #my_app/app/views/service_types/index.html.erb
 <h1>Listing service_types</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Time allotment</th>
  </tr>

<% @service_types.each do |service_type| %>
  <tr>
    <td><%=h service_type.title %></td>
    <td><%=h service_type.time_allotment %></td>
    <td><%= link_to 'Show', service_type %></td>
    <td><%= link_to 'Edit', edit_service_path(service_type) %></td>
    <td><%= link_to 'Destroy', service_type, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New service_type', new_service_path %>

-

#my_app/app/views/service_types/new.html.erb
<h1>New service_type</h1>

<% form_for(@service_type) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :time_allotment %><br />
    <%= f.text_field :time_allotment %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', services_path %>

当您尝试访问 http://localhost:3000/services/new 您会收到以下错误:

undefined method `service_types_path' for #<ActionView::Base:0xb7199a80>

提取的源代码(围绕第 #3 行):

1: <h1>New service_type</h1>
2: 
3: <% form_for(@service_type) do |f| %>
4:   <%= f.error_messages %>
5: 
6:   <p>

应用程序跟踪:

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `__send__'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `polymorphic_url'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:114:in `polymorphic_path'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:298:in `apply_form_for_options!'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for'
/home/aaron/NetBeansProjects/my_app/app/views/service_types/new.html.erb:3:in `_run_erb_app47views47service_types47new46html46erb'
/home/aaron/NetBeansProjects/my_app/app/controllers/service_types_controller.rb:29:in `new'

任何人都知道为什么它认为 service_types_path 在我的代码中,但它不是?

ok here's some code:

prompt>rails my_app
prompt>cd my_app
prompt>script/generate scaffold service_type title:string time_allotment:integer
prompt>rake db:migrate

then edit these files to look like this:

#routes.rb:
ActionController::Routing::Routes.draw do |map|
  map.resources :services, :controller => :service_types
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

which produces these routes:

prompt>rake routes
    services GET    /services(.:format)                {:controller=>"service_types", :action=>"index"}
             POST   /services(.:format)                {:controller=>"service_types", :action=>"create"}
 new_service GET    /services/new(.:format)            {:controller=>"service_types", :action=>"new"}
edit_service GET    /services/:id/edit(.:format)       {:controller=>"service_types", :action=>"edit"}
     service GET    /services/:id(.:format)            {:controller=>"service_types", :action=>"show"}
             PUT    /services/:id(.:format)            {:controller=>"service_types", :action=>"update"}
             DELETE /services/:id(.:format)            {:controller=>"service_types", :action=>"destroy"}
                    /:controller/:action/:id           
                    /:controller/:action/:id(.:format)

_

 #my_app/app/views/service_types/index.html.erb
 <h1>Listing service_types</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Time allotment</th>
  </tr>

<% @service_types.each do |service_type| %>
  <tr>
    <td><%=h service_type.title %></td>
    <td><%=h service_type.time_allotment %></td>
    <td><%= link_to 'Show', service_type %></td>
    <td><%= link_to 'Edit', edit_service_path(service_type) %></td>
    <td><%= link_to 'Destroy', service_type, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New service_type', new_service_path %>

-

#my_app/app/views/service_types/new.html.erb
<h1>New service_type</h1>

<% form_for(@service_type) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>
  <p>
    <%= f.label :time_allotment %><br />
    <%= f.text_field :time_allotment %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', services_path %>

when you try to access http://localhost:3000/services/new you get the following error:

undefined method `service_types_path' for #<ActionView::Base:0xb7199a80>

Extracted source (around line #3):

1: <h1>New service_type</h1>
2: 
3: <% form_for(@service_type) do |f| %>
4:   <%= f.error_messages %>
5: 
6:   <p>

Application Trace:

/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `__send__'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `polymorphic_url'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:114:in `polymorphic_path'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:298:in `apply_form_for_options!'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for'
/home/aaron/NetBeansProjects/my_app/app/views/service_types/new.html.erb:3:in `_run_erb_app47views47service_types47new46html46erb'
/home/aaron/NetBeansProjects/my_app/app/controllers/service_types_controller.rb:29:in `new'

Anyone have any idea why it believes that service_types_path is in my code when it's not?

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

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

发布评论

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

评论(1

失眠症患者 2024-09-08 09:09:45
<% form_for(@service_type) do |f| %>

删除与

<% form_for(@service_type, :url => services_path, :html => { :method => :post }) do |f| %>
<% form_for(@service_type) do |f| %>

remove with

<% form_for(@service_type, :url => services_path, :html => { :method => :post }) do |f| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文