1
私は2つのモデルを持っています。RailsマップされたフィールドのIDをドロップダウンリストに入れる方法
Countries
name:string
と
Descriptions
title:string
text:string
country_id:integer
` 私は、ドロップダウンリストから、国を選択したときの説明に国のIDを追加する方法?
<%= form_for @description, :url => {:action => "create", :id => "#{@tour.id}" }, :html => { :multipart => true } do |f| %>
<%= f.label :Title %><br/>
<%= f.text_field :title %><br/>
<%= f.label :Desc %><br/>
<%= f.text_field :text %><br/>
<%= f.select :city, (@cities.map { |l| [l.name, l.name] }) %>
<%= f.submit "Add"%>
<% end %>
コントローラ
def new
@description = Description.new
@tour = Tour.find(params[:id])
@cities = City.all
...
end
def create
@tour = Tour.find(params[:id])
@description = @tour.descriptions.build(description_params)
end
私はあなたがcollection_selectを使用して試すことができます任意のヒントや便利なリンク:)
ありがとうございます。 – Yurii10
私の喜びです。お役に立てて嬉しいです。 –