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.

4

1 回答 1

1

对于选择框:

您应该使用collection_select表单助手。

代替

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

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

用于在区域的显示视图上显示电台列表:

有几种方法可以做到这一点,具体取决于您希望如何在 HTML 中显示它们。

这是获取以逗号分隔的电台名称列表的一种方法:

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

如果您需要除了 just 之外的站点的其他属性,则:name可以迭代集合而不是使用 pluck:

<ul>
  <% @area.stations.each do |s| %>
    <li><%= s.name %> - <%= s.frequency %></li>
  <% end %>
</ul>
于 2012-05-28T22:14:38.033 回答