<%= 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
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_forin the new.html.erb file you’ll seeequipment_pathin the link_to at the bottom. For these models with singular==plural names that refers to a route that is actually for theshowaction, 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 %>