I am sorry for asking a beginner s question. I can t get a "collection_select" to work in a specific case.
试图根据以下模式撰写一条简单的铁路3.1:
class Site < ActiveRecord::Base
has_many :supply_sites, :dependent => :destroy
has_many :demand_sites, :dependent => :destroy
accepts_nested_attributes_for :supply_sites
accepts_nested_attributes_for :demand_sites
end
class DemandSite < ActiveRecord::Base
belongs_to :site, :class_name => "Site"
has_many :translinks , :dependent => :destroy
end
class SupplySite < ActiveRecord::Base
belongs_to :site, :class_name => "Site"
has_many :translinks , :dependent => :destroy
end
class Translink < ActiveRecord::Base
belongs_to :supply_site, :class_name => "SupplySite"
belongs_to :demand_site, :class_name => "DemandSite"
end
Migrations are as follows:
class CreateSites < ActiveRecord::Migration
def self.up
create_table :sites do |t|
t.string :name
t.string :codename, :limit => 3
t.timestamps
end
end
def self.down
drop_table :sites
end
end
class CreateSupplySites < ActiveRecord::Migration
def self.up
create_table :supply_sites do |t|
t.integer :site_id
t.float :supply_quantity
t.timestamps
end
end
def self.down
drop_table :supply_sites
end
end
class CreateDemandSites < ActiveRecord::Migration
def self.up
create_table :demand_sites do |t|
t.integer :site_id
t.float :demand_quantity
t.timestamps
end
end
def self.down
drop_table :demand_sites
end
end
class CreateTranslinks < ActiveRecord::Migration
def self.up
create_table :translinks do |t|
t.integer :supply_site_id
t.integer :demand_site_id
t.float :unit_cost
t.float :quantity
t.timestamps
end
end
def self.down
drop_table :translinks
end
end
I want to be able to add a new "translink" (Transportation Link) between a "Supply Site" and a "Demand Site by selecting from a Drop-Down menu based on the Codenames of the respective Supply Site or Demand Site, where the codenames are specfified for the "Sites".
When adding a new "Supply Site" (or a "Demand Site"), the following works well (from "_form.html.erb" for either Supply Sites or Demand Sites.
<div class="field">
<%= f.label :site_id %><br />
<%= f.collection_select :site_id, Site.find(:all), :id, :codename %>
</div>
Now I want something analogous for for adding a new "Translink" connecting a Supply Site and a Demand Site. I don t want to add the respective supply_site_id or demand_site_id manually, but a list of all supply sites to select one by the codename defined in the underlying site, and the same for the demand sites. I can do the following to to have a drop-down menu to select, say, one out of the existing supply sites:
<div class="field">
<%= f.label :supply_site_id %><br />
<%= f.collection_select :supply_site_id, SupplySite.find(:all), :id, :id %>
</div>
然而,我不但没有在下降的菜单中显示供应点的宽度,而是会看到并选择基本“现场”的代码名称。
<div class="field">
<%= f.label :supply_site_id %><br />
<%= f.collection_select :supply_site_id, SupplySite.find(:all), :id ,????? %>
</div>
How can I do this?
任何帮助都将受到高度赞赏。
Stefan