【问题标题】:ruby on rails uninitialized constant error nested resourcesruby on rails 未初始化常量错误嵌套资源
【发布时间】:2014-10-15 00:45:06
【问题描述】:

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

未初始化的常量State::County

列出的错误代码来自/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 文件和 DB 架构。 楷模: 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

县.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

结束

感谢任何帮助...

【问题讨论】:

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


    【解决方案1】:

    没关系...我找到了自己问题的答案。我知道这一定很简单。我在 County.rb 文件中遗漏了一个冒号。

    原文:

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

    更正:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多