我试图在RoR中使用form_for帮助器生成一个表单,但是我遇到似乎是一个路由错误.以下是相关文件:
车型/ equipment.rb
class Equipment < ActiveRecord::Base attr_accessible :name,:tracking_number validates :tracking_number,:presence => true,:uniqueness => { :case_sensitive => true } end
控制器/ equipments_controllers.rb
class EquipmentsController < ApplicationController def index @equipments = Equipment.paginate(:page => params[:page]) end def new @equipment = Equipment.new end end
视图/设备/ 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
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>
如果我把它改成了
<%= form_for (:equipment) do |f| %>
它似乎工作正常.我也确定静态变量@equipment从此开始传递
<%= @equipment %>
回报
#<Equipment:0xb685ece0>
我在这里亏了我刚刚完成了我在跟踪railstutorial.org的书时所做的一切,我能够完成这本书.
解决方法
我想你的问题在于你使用“设备”一词.如果您打开Rails控制台运行’equipment’.pluralize,您会看到复数“设备”是“设备”.
所以我会通过你的项目搜索,并用“设备”替换任何“设备”的实例,我敢打赌这将修复它.