ruby on rails uninitialized constant error嵌套资源

时间:2014-10-15 00:45:06

标签: ruby-on-rails ruby-on-rails-3.2

我是Ruby on Rails的新手,并且多年没有编程。我尝试了一些类似于" Rails入门"中提供的示例的简单代码。 Rails指南3.2。而不是帖子和评论,我的模型是州和县。我已经审查了复数问题的代码,但没有看到任何不合适的地方。我的系统配置了Rails 4.0和ruby 1.9.3。我从索引页面列出状态后遇到错误。一旦列出状态,我选择显示一个状态,这应该允许我添加一个县,但我在页面上得到以下错误:

未初始化的常量州::县

错误列出的代码来自/app/views/states/show.html.erb

    </p>
    <h2>Add a County:</h2>
    <%= form_for([@state, @state.counties.build]) do |f| %>
       <div class="field">
         <%= f.label :name %><br />
         <%= f.text_field :name %>

我在下面提供其他MVC文件和数据库架构。 楷模: state.rb

    class State < ActiveRecord::Base  
      attr_accessible :abbr, :name
      validates :abbr, :presence => true,
               :length => { :maximum => 2 },
               :format => { :with => /\A[A-Z]+\z/,
                  :message => "only 2 uppercase letters allowed" }                    
      validates :name, :presence => true
      has_many :counties, :dependent => :destroy                   
    end

county.rb

    class County < ActiveRecord::Base
      attr_accessible :name, state_id
      validates :name, :presence => true
      belongs_to :state
    end

浏览 状态/ show.html.erb

    <p id="notice"><%= notice %></p>
    <p>
      <strong>Abbr:</strong>
      <%= @state.abbr %>
    </p>
    <p>
      <strong>Name:</strong>
      <%= @state.name %>
    </p>
    <h2>Add a County:</h2>
    <%= form_for([@state, @state.counties.build]) do |f| %>
      <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="actions">
      <%= f.submit %>
    </div>
    <% end %>
    <br />
    <%= link_to 'Edit', edit_state_path(@state) %> |
    <%= link_to 'Back', states_path %>

的routes.rb

    resources :states do
      resources :counties
    end

控制器

counties_controller.rb

    class CountiesController < ApplicationController
      def create
        @state = State.find(params[:state_id])
        @county = @state.counties.create(params[:county])
        redirect_to state_path(@state)
      end
      def destroy
        @state = State.find(params[:state_id])
        @county = @state.counties.find(params[:id])
        @county.destroy
        redirect_to state_path(@state)
      end
    end

states_controller.rb 这是带有脚手架生成器的rails创建的标准文件。没有对此文件进行任何更改。如果你需要它来帮助解决这个问题,我会发布它,但它很长。

schema.rb

    ActiveRecord::Schema.define(version: 20141013234441) do
      create_table "counties", force: true do |t|
        t.string   "name"
        t.integer  "state_id"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
      add_index "counties", ["state_id"], name: "index_counties_on_state_id", using: :btree

      create_table "states", force: true do |t|
        t.string   "abbr",       limit: 2, null: false
        t.string   "name"
        t.datetime "created_at"
        t.datetime "updated_at"
      end

感谢任何帮助...

1 个答案:

答案 0 :(得分:0)

没关系......我找到了自己问题的答案。我知道它必须简单。我在county.rb文件中缺少一个冒号。

原件:

class County < ActiveRecord::Base
  attr_accessible :name, state_id

修正:

class County < ActiveRecord::Base
  attr_accessible :name, :state_id