Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method "_index_path" form_for problem

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.

like image 240
Rystraum Avatar asked Feb 24 '11 03:02

Rystraum


1 Answers

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.

like image 63
aNoble Avatar answered Nov 01 '22 22:11

aNoble