11

I'm trying to generate a form using the form_for helper in RoR but I am encountering what seems to be a routing error. Here are the relevant files:

models/equipment.rb

class Equipment < ActiveRecord::Base
  attr_accessible :name, :tracking_number
  validates :tracking_number, :presence => true,
                              :uniqueness => { :case_sensitive => true }
end

controllers/equipments_controllers.rb

class EquipmentsController < ApplicationController
  def index
    @equipments = Equipment.paginate(:page => params[:page])
  end

  def new
    @equipment  = Equipment.new
  end

end

views/equipments/new.html.rb

<h1>Add an equipment</h1>
<%= form_for (@equipment) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.label :name %> <br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :tracking_number %><br />
    <%= f.text_field :tracking_number %>
  </div>
  <%= f.submit "Add" %>
<% end  %>

routes.rb

EquipmentTracking::Application.routes.draw do
  root :to => "equipments#index"
  resources :equipments
end

I don't see anything wrong but they output the following:

NoMethodError in Equipments#new
Showing /opt/ror/equipment_tracking/app/views/equipments/new.html.erb where line #2 raised:
undefined method `equipment_index_path' for #<#<Class:0xb6725a2c>:0xb6724640>

If I changed it to

<%= form_for (:equipment) do |f| %>

it seems to work ok. I'm also certain that the static variable @equipment is getting passed since

<%= @equipment %>

returns

#<Equipment:0xb685ece0> 

I am at a loss here. I just did what I did while I was following the railstutorial.org book and I was able to finish the book.

1
  • Is there any more content in those files? equipment_index_path is rather peculiar and shouldn't be a routing path generated by Rails. Commented Feb 24, 2011 at 3:36

1 Answer 1

25

I think your problem lies in your use of the word "equipments". If you open the Rails console run 'equipment'.pluralize you'll see that the plural of "equipment" is "equipment".

So I'd do a search through your project and replace any instance of "equipments" with "equipment" and I'd bet that would fix it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did it. I should keep in mind to check the rails pluralize before making my controllers then.
I'd do better to check up on my vocabulary and my collective nouns too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.