0

Department モデル、Course モデル、および Mandatory モデルがあります。各部門には必須のコースがいくつかあるため、Mandatory モデルを使用して Courses と Department モデルをマッピングします。

IDの代わりにコースと部門の名前を選択して、「必須」の関係を作成できるようにしたいと考えています。

2 つのインスタンス変数 @departments(Department.all) と @courses(Course.all) を作成し、ビューでそれらをドロップダウンとして表示して、1 つの部門、1 つのコースを選択し、必須の関係を作成すると考えました。私が望んでいたほど単純ではないことがわかりました。collection_selectヘルパー メソッドの使用方法がわかりません。ドキュメントを読みましたが、理解できません。

だから私の部門モデルは

class Department < ActiveRecord::Base
attr_accessible :industry_name, :name

has_many :employees
has_many :mandatories
has_many :courses, :through => :mandatories
end

コースモデル

class Course < ActiveRecord::Base
attr_accessible :name

has_many :registrations
has_many :mandatories
has_many :employees, :through => :registrations
has_many :departments, :through => :mandatories
end

必須モデル

class Mandatory < ActiveRecord::Base
attr_accessible :course_id, :department_id

belongs_to :course
belongs_to :department
end

ドキュメントから得た構文は<%= collection_select(:person, :city_id, City.all, :id, :name) %>

これはまったく理解できません。

編集::

だから、これが私の現在の見解です。まだありませcollection_selectん。今、部門とコースの間の関係を作成するには、IDを入力する必要がありますが、これはやりたくないことです。代わりに、部門名とコース名のドロップダウンが欲しいです。

これは私_form.htmlの必須コントローラーです

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

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

<div class="field">
  <%= f.label :department_id %><br />
  <%= f.number_field :department_id %>
</div>
<div class="field">
  <%= f.label :course_id %><br />
  <%= f.number_field :course_id %>
</div>

<div class="actions">
  <%= f.submit %>
</div>
<% end %>

そして、ここに必須のコントローラーがあります

def create
@mandatory = Mandatory.new(params[:mandatory])
@courses = Course.all
@departments  =Department.all
respond_to do |format|
  if @mandatory.save
    format.html { redirect_to @mandatory, notice: 'Mandatory was successfully created.' }
    format.json { render json: @mandatory, status: :created, location: @mandatory }
  else
    format.html { render action: "new" }
    format.json { render json: @mandatory.errors, status: :unprocessable_entity }
  end
end
end
4

2 に答える 2

1

次のようなフォームのコンテキストで使用する必要があります。

<% form_for @mandatory do |f| %>

  <div class="field">
    <%= f.label :course_id %><br />
    <%= f.collection_select(:course_id, Course.all, :id, :name) %>
  </div>

  <%= f.submit %>
<% end %>
于 2013-08-13T13:03:30.313 に答える
1
<%= collection_select(:person, :city_id, City.all, :id, :name) %>

部品を見てみましょう:

:personコレクションから 1 つのアイテムを含むモデルの名前です。

:city_idコレクションのアイテムを含むモデル内のフィールドの名前です。

City.allコレクション内のアイテムのリストです。@citiesコントローラ ( ) でデータベース要求を行った場合、これはインスタンス変数 ( ) になる可能性があります@cities = City.all。コレクションで必要なレコードのサブセットのみを取得することもできます ( @cities = City.where("state = ?", ["NY", "SC", "NE"]))

:idおよびは、選択リストの場合に各行で使用されるオブジェクト:nameのフィールドです。は値であり、ドロップダウン リストの各オプションのテキストです。City:id:name

生成された HTML は次のようになります。

<select name="person[city_id]">
<option value="1">New York</option>
<option value="3">Rome</option>
<option value="4">Charleston</option>
<option value="17">Omaha</option>
</select>

編集:選択の「id」は、私が見ていないときに脱落したように見えるので、削除しました。

于 2013-08-13T13:30:21.643 に答える