Aliasing a route causes rails to expect paths that don't exist
- by DJTripleThreat
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?