对路由进行别名会导致rails期望不存在的路径

时间:2010-05-12 01:13:16

标签: ruby-on-rails controller views routes alias

好的,这里有一些代码:

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不在我的代码中?

1 个答案:

答案 0 :(得分:1)

<% form_for(@service_type) do |f| %>

删除

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