未定义的方法“_index_path"form_for 问题 [英] undefined method "_index_path" form_for problem

查看:49
本文介绍了未定义的方法“_index_path"form_for 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RoR 中的 form_for 帮助程序生成一个表单,但我遇到了似乎是路由错误的问题.以下是相关文件:

models/equipment.rb

类装备真的,:唯一性 =>{ :case_sensitive =>真的 }结尾

controllers/equipments_controllers.rb

class EquipmentsController <应用控制器定义索引@equipments = Equipment.paginate(:page => params[:page])结尾定义新@equipment = Equipment.new结尾结尾

views/equipments/new.html.rb

添加一个设备

<%= form_for (@equipment) 做 |f|%><%= 渲染 'shared/error_messages', :object =>f.对象%><div class="field"><%= f.label :name %><br/><%= f.text_field :name %>

<div class="field"><%= f.label :tracking_number %><br/><%= f.text_field :tracking_number %>

<%= f.submit "Add" %><%结束%>

routes.rb

EquipmentTracking::Application.routes.draw 做根:到 =>设备#index"资源:设备结尾

我没有看到任何错误,但他们输出以下内容:

设备中没有方法错误#new显示/opt/ror/equipment_tracking/app/views/equipments/new.html.erb 其中第 2 行提出:#<#<Class:0xb6725a2c>:0xb6724640>的未定义方法`equipment_index_path'

如果我把它改成

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

好像没问题.我也确定静态变量 @equipment 从

开始被传递

<%= @equipment %>

返回

#

我在这里不知所措.我只是按照 railstutorial.org 的书做了我所做的,并且我能够完成这本书.

解决方案

我认为你的问题在于你对设备"这个词的使用.如果你打开 Rails 控制台运行 'equipment'.pluralize 你会看到equipment"的复数是equipment".

所以我会在你的项目中搜索并用设备"替换任何设备"实例,我敢打赌这会解决它.

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.

这篇关于未定义的方法“_index_path"form_for 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆