0

「railsgscaffoldModelName type:attribute1、type2:attribute2」コマンドによって生成された2つの「CRUD」フォームがあります。これは非常に強力です。

何が関連しているのかを示してみます。最初に私のモデルがあります(attr_accessibleはほとんどの部分でdb移行ストーリーを伝えます)

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

次に、Stationオブジェクトの_form.html.erbを示します(現在、単純なドロップダウンを使用していますが、これらの:area_idタグでArea.find(params [:area_id ])。name、またはそのようなもの。「1」がデンバーで「2」がボルダーの場合、Station_formで「1」に「デンバー」などをプルさせます。

これがstations_controller.rbで、ほとんどの場合「足場の生成」コマンドによって生成されました。

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

最後に、Stationの_form.html.erbです。

<%= 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 %>

したがって、私の目標/質問を繰り返すために、現在:area_idセレクターに使用しているのは、1.."Area.count"から選択できるドロップダウンバーです。そのドロップダウンバーに、area_idで指定されたさまざまなエリアの名前を一覧表示したいと思います。

また、エリアのリストまたは特定のエリアのshow.html.erbで、特定のエリアが「所有」しているStationオブジェクトのリストを表示できるようにしたいと思います。

4

1 に答える 1

1

選択ボックスの場合:

collection_selectフォームヘルパーを使用する必要があります。

交換

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

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

エリアのショービューにステーションのリストを表示するには:

HTMLでの表示方法に応じて、これを行うにはいくつかの方法があります。

ステーション名のコンマ区切りリストを取得する1つの方法は次のとおりです。

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

だけでなくステーションの追加の属性が必要な場合は、:namepluckを使用するのではなく、コレクションを反復処理できます。

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