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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:52:21+00:00 2026-06-04T18:52:21+00:00

I have two CRUD forms generated by the rails g scaffold ModelName type:attribute1, type2:attribute2

  • 0

I have two “CRUD” forms generated by the “rails g scaffold ModelName type:attribute1, type2:attribute2” command, which is quite powerful.

I’ll try to just show what is relevant. First here are my models (attr_accessible tells the db migration stories for the most part)

class Area < ActiveRecord::Base
  attr_accessible :description, :frequency, :name, :area_id
  has_many :stations
end


class Station < ActiveRecord::Base
  attr_accessible :description, :frequency, :name, :area_id
  belongs_to :area
end

Next, here is the _form.html.erb for a Station object (currently I’m using a simple drop down which is OK, but I want those :area_id tags to somehow be able to pull the Area.find(params[:area_id]).name, or something like that. If ‘1’ is Denver and ‘2’ is Boulder, I want ‘1` to pull ‘Denver’ and so on on the Station _form.

So here is stations_controller.rb, which was generated by the “generate scaffold” command for the most part.

class StationsController < ApplicationController

  def index

  @stations = Station.all

    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @stations }

 end

 end

  def show
    @station = Station.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @station }
    end
  end

  def new
    @area_count = Area.count
    @station = Station.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @station }
    end
  end


  def edit
    @station = Station.find(params[:id])
  end

  def create
   @station = Station.new(params[:station])

    respond_to do |format|
      if @station.save
        format.html { redirect_to @station, notice: 'Station was successfully created.' }
        format.json { render json: @station, status: :created, location: @station }
  else
        format.html { render action: "new" }
        format.json { render json: @station.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @station = Station.find(params[:id])

    respond_to do |format|
      if @station.update_attributes(params[:station])
        format.html { redirect_to @station, notice: 'Station was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @station.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @station = Station.find(params[:id])
    @station.destroy

    respond_to do |format|
      format.html { redirect_to stations_url }
      format.json { head :no_content }
    end
  end
end

Lastly, here is the _form.html.erb for Station

<%= form_for(@station) do |f| %>
  <% if @station.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@station.errors.count, "error") %> prohibited this station from     being saved:</h2>

      <ul>
      <% @station.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :frequency %><br />
   <%= f.text_field :frequency %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :area_id %><br />
    <%= f.select(:area_id, 1..@area_count) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

So to reiterate my goal/question, what I have currently for the :area_id selector is a dropdown bar which allows me to select from 1..”Area.count”. I would like that drop down bar to list the names of the the different areas given the area_id.

Also, I’d like to be able to see a list of Station objects which are “owned” by a given area on the list of areas or perhaps just on the show.html.erb for a given Area.

  • 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-06-04T18:52:22+00:00Added an answer on June 4, 2026 at 6:52 pm

    For the select box:

    You should use the collection_select form helper.

    Replace

    <%= f.select(:area_id, 1..@area_count) %>
    

    With

    <%= f.collection_select :area_id, Area.all, :id, :name %>
    

    For showing the list of stations on the show view of an area:

    There are several ways to do this, depending on how you want to display them in the HTML.

    Here’s one way to obtain a comma separated list of the station names:

    <%= @area.stations.pluck(:name).join(", ") %>
    

    If you need additional attributes of the stations besides just :name, you could iterate over the collection rather than using pluck:

    <ul>
      <% @area.stations.each do |s| %>
        <li><%= s.name %> - <%= s.frequency %></li>
      <% end %>
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very standard CRUD app in Rails & Jquery Mobile with two
I have a Ruby/Rails app, which is standard Rails CRUD, with some jQuery and
I have a web application that uses two databases. DB1 Users perform their CRUD
I have two classes which need to be in same xml file. The way
As the title suggests I have two models Products And Orders which are actually
I have a pawnshop CRUD app written 20 years ago with INFORMIX-SQL/SE (DOS) which
I have a web solution (in VS2010) with two sub-projects: Domain which holds the
I have two models : Category and Picture which refers to two tables, Categories
In my project rails have a CRUD for Users, when i am in the
Have two folders with approx. 150 java property files. In a shell script, how

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.