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 7192827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:02:00+00:00 2026-05-28T20:02:00+00:00

I am having issues with my Rails 3.1 app. I have two controllers &

  • 0

I am having issues with my Rails 3.1 app. I have two controllers & models, named counties/county and towns/town.

The county model has has_many :towns and the town model has belongs_to :county.

I am trying to show the County name on the Towns index page to show the title as ‘Towns in -county-name-‘. However, when I put %h1 Towns in #{@towns.county.name} I get a undefined method 'county' error.

The towns controller for index is

def index
    @towns = Town.all
end

For the county controller:

def index
    @counties = County.all
end

My routes.rb is

resources :counties, :path => "/locations" do
    resources :towns, :path => "/"
end

What am I doing wrong?

EDIT: Using this in my towns/index view and it shows the town.county.id and town.county.name:

- @towns.each do |town|
  %tr
    %td= town.county.id
    %td= town.county.name
    %td= town.name
    %td= town.description
    %td= town.slug
  • 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-28T20:02:02+00:00Added an answer on May 28, 2026 at 8:02 pm

    I had a similar problem with states and countries (vs town and counties).
    For your town migration, did you have an integer to reference the county (see county_id below)

    class CreateTowns < ActiveRecord::Migration
      def change
        create_table :towns do |t|
          t.string :name
          t.integer :county_id
    
          t.timestamps
        end
      end
    end
    

    Then in your app/views/town/index.html.erb you can do a County.find

     %tr
        %td= town.county.id
        %td= County.find(town.county_id).name
        %td= town.name
        %td= town.description
        %td= town.slug
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some issues with routing in Rails. I already have a model in
As usual, I'm having strange issues with deploying a Rails app to our different
I have been experiencing an intermittent issue in my Rails app, and I'm having
Having issues with a rails 2.2.2 app running on a VPS (Ubuntu 8.10): looking
I'm having an issue with rails involving javascript. Basically, I have the following code:
Im having issues getting this to work, maybe its not even possible? I have
I am having an issue with namespaces in Rails 3.1. I have a class,
I'm having a rough time with Apache and my Rails app on my production
In a Rails 3.1 app, I have a controller returning a set of objects
I am developing an issue tracker app in Rails 3 but I am having

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.