5
<%= link_to t('.new', :default => t("helpers.links.new")), new_equipment_path, :class => 'btn btn-primary' %>

I have the above code in a view, but am getting the following error when clicking the link: No route matches {:action=>"show", :controller=>"equipment"}

My routes file contains:

resources :equipment

resources :workouts

match ':controller(/:action(/:id))(.:format)'

Why is it trying to access the show action?

Here are the entries from my routes:

   equipment_index GET        /equipment(.:format)                   equipment#index
                   POST       /equipment(.:format)                   equipment#create
     new_equipment GET        /equipment/new(.:format)               equipment#new
    edit_equipment GET        /equipment/:id/edit(.:format)          equipment#edit
         equipment GET        /equipment/:id(.:format)               equipment#show
                   PUT        /equipment/:id(.:format)               equipment#update
                   DELETE     /equipment/:id(.:format)               equipment#destroy
Nick5a1
  • 917
  • 3
  • 15
  • 28
  • I suspect your t() block is confusing things — does it work when you change that to a normal string? Like: `<%= link_to "testing...", new_equipment_path, :class => 'btn btn-primary' %>` – MBHNYC Jul 07 '12 at 02:41
  • Yeah it's doing the same thing for a normal link_to – Nick5a1 Jul 07 '12 at 03:33
  • 1
    Can you please edit your question to include the output of running `rake routes`? – iwasrobbed Jul 07 '12 at 08:32

3 Answers3

6

This issue has cropped up before and is related to how rails scaffolding generates the new.html.erb file for models that have names like 'equipment' which are both singular and plural.

If you inspect the form_for in the new.html.erb file you'll see equipment_path in the link_to at the bottom. For these models with singular==plural names that refers to a route that is actually for the show action, hence your error message.

The advice is often along the lines of 'avoid model names like this if you can' or it involves a bit of messing around with the config/initializers/inflections.rb file to force a plural version of the model name. Of course then you end up with an app with very odd sounding references to models: 'equipments' isn't very nice to work with (and someone later on will 'fix' it, messing things up again).

To keep the model name grammatically correct, you need to fix the form_for i.e.:

<% form_for(@equipment, :url=> {:action=>'create'}) do |f| %>

and the link_to:

<%= link_to 'Back', equipment_index_path %>

edralph
  • 1,831
  • 1
  • 14
  • 14
0

Have you tried adding an 's' to equiment in your routes.rb?

resources :equipments
Wawa Loo
  • 2,266
  • 18
  • 15
0

Since equipment is considered an uncountable noun, you should be able to solve this using inflections.

The rails documentation on inflections actually uses equipment as part of its' example.

Add the following to your config/initializers/inflections.rb.

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable 'equipment'
end

Additional information: Uncountable Nouns in Rails 3 Resource Routing

cweston
  • 11,297
  • 19
  • 82
  • 107