Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6696063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:17:48+00:00 2026-05-26T06:17:48+00:00

I am working on a prototyp (rails 2.2.2) to create a similiar page structure

  • 0

I am working on a prototyp (rails 2.2.2) to create a similiar page structure as the business directory from http://www.redbeacon.com/s/b/.

Goal should be to have the following paths: mysite.com/d/state/location/ …which displays an index of something. So far, I did the following…

Controllers and models:

$ ruby script/generate controller Directories index show
$ ruby script/generate controller States index show
$ ruby script/generate controller Locations index show
$ ruby script/generate model State name:string abbreviation:string
$ ruby script/generate model Location name:string code:string state_id:integer
$ rake db:migrate

routes:

map.states '/d', :controller => 'states', :action => 'index'
map.locations '/d/:state', :controller => 'locations', :action => 'index'
map.directories '/d/:state/:location', :controller => 'directories', :action => 'index'

…built in the models the relations:

class State < ActiveRecord::Base
  has_many :locations
end

class Location < ActiveRecord::Base
  belongs_to :states
end

…added actions to the controllers:

class StatesController < ApplicationController
  def index
    @all_states = State.find(:all)
  end
end

class LocationsController < ApplicationController
 def index
    @all_locations = Location.find(:all)
    @location = Location.find_by_id(params[:id])
  end
end

class DirectoriesController < ApplicationController
  def index
    @location = Location.find_by_id(params[:id])
    @all_tradesmen = User.find(:all)
  end
end

The States Index View

<h1>States#index</h1>
<p>Find me in app/views/states/index.html.erb</p>
<br><br>
<% for state in @all_states %>
  <%= link_to state.name, locations_path(state.abbreviation.downcase) %>
<% end %>

The Locations Index View

<h1>Locations#index</h1>
<p>Find me in app/views/locations/index.html.erb</p>
<br><br>

<% for location in @all_locations %>
  <%= link_to location.name, directories_path(location.state.abbreviation, location.name) %>
<% end %>

But I am stuck, I get the following error message:

NoMethodError in Locations#index

Showing app/views/locations/index.html.erb where line #6 raised:

undefined method `state' for #<Location:0x104725920>

Extracted source (around line #6):

3: <br><br>
4: 
5: <% for location in @all_locations %>
6:   <%= link_to location.name, directories_path(location.state.abbreviation, location.name) %>
7: <% end %>

Any Ideas why this error message pops up? Or generally any ideas for a better approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:17:49+00:00Added an answer on May 26, 2026 at 6:17 am

    The part of the code you should look at is:

    class Location < ActiveRecord::Base
      belongs_to :states
    end
    

    and it should be

    class Location < ActiveRecord::Base
      belongs_to :state
    end
    

    Another note, although not related to the error you are getting, Ruby programmers usually prefer array.each than for item in array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
I'm following this tutorial http://ruby.railstutorial.org/ The tutorial seems to be using Rails 3.0. I
I am working on Ruby on Rails application, with Prototype. I want to display
I am new to rails, and working on an internal content management app. I
I am working on a project right now that involves receiving a message from
I've installed https://github.com/lleger/Rails-3-jQuery and it worked fine, however, the rails helpers that I was
I'm working on rails tutorial by Michael Hartl like other newbies in rails do.
This is all new territory for me, but I am working through a Rails
I'm working with my Rails 3.1 migration and it's getting harder and harder to
I'm working on a prototype in Rails 3 and ended up using some jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.