rails link path and routing error when model singular and plural name are the same (e.g. equipment, species) -


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

i have above code in view, getting following error when clicking link: no route matches {:action=>"show", :controller=>"equipment"}

my routes file contains:

resources :equipment resources :workouts match ':controller(/:action(/:id))(.:format)' 

why trying access show action?

here entries routes:

 equipment_index /equipment(.:format) equipment#index post /equipment(.:format) equipment#create new_equipment /equipment/new(.:format) equipment#new edit_equipment /equipment/:id/edit(.:format) equipment#edit equipment /equipment/:id(.:format) equipment#show put /equipment/:id(.:format) equipment#update delete /equipment/:id(.:format) equipment#destroy 

this issue has cropped before , related how rails scaffolding generates new.html.erb file models have names 'equipment' both singular , plural.

if inspect form_for in new.html.erb file you'll see equipment_path in link_to @ bottom. these models singular==plural names refers route show action, hence error message.

the advice along lines of 'avoid model names if can' or involves bit of messing around config/initializers/inflections.rb file force plural version of model name. of course end app odd sounding references models: 'equipments' isn't nice work (and later on 'fix' it, messing things again).

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

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

and link_to:

<%= link_to 'back', equipment_index_path %>


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -