I'm new to Ruby on Rails and have not programmed for many years. I'm trying some simple code that is similar to the sample provided in the "Getting Started with Rails" guide for Rails 3.2. Instead of Posts and Comments my models are States and Counties. I've reviewed the code for problems with pluralization, but don't see anything out of place. My system is configured with Rails 4.0 and ruby 1.9.3. I encounter the error after I list the states from the index page. Once the states are listed, I select to show a state, which should allow me to add a County, but instead I get the following error on the page:
uninitialized constant State::County
The code listed with the error is from /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 %>
I'm providing other MVC files and the DB schema below. Models: 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
Views State/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
Controllers
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 This is a standard file created by rails with the scaffold generator. No changes have been made to this file. If you need it to help with this issue I will post it, but it is rather long.
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
end
Any help is appreciated...
Never mind... I found the answer to my own question. I knew it had to be something simple. I was missing a colon in the county.rb file.
Original:
class County < ActiveRecord::Base
attr_accessible :name, state_id
Corrected:
class County < ActiveRecord::Base
attr_accessible :name, :state_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.