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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With