English 中文(简体)
铁路收集3.1 选择两个以上职等? (带式模型)
原标题:Rails 3.1 collection_select over two levels? (nested models)

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

问题回答

填表/代码 它使你能够做这样的工作,以具备以下特征:

<%= f.fields_for(options) do |nested_f|
  nested_f.text_field :nested_attribute

感谢Katen!

解决办法是,在需求地点类别和供应地点类别中增加以下方法:

class SupplySite < ActiveRecord::Base
  belongs_to :site, :class_name => "Site"
  has_many :translinks , :dep结束ent => :destroy
  accepts_nested_attributes_for :translinks


 def supply_site_codename
   self.site.codename
 结束

结束

class DemandSite < ActiveRecord::Base
    belongs_to :site, :class_name => "Site"
    has_many :translinks , :dep结束ent => :destroy
    accepts_nested_attributes_for :translinks


  def demand_site_codename
     self.site.codename
  结束

 结束

这样,我就可使用标准“收集-选择”和参考:基本“现场”的编码名称如下:

<div class="field">
   <%= f.label :supply_site_id %>
   <br/>
   <%= f.collection_select :supply_site_id, SupplySite.find(:all), :id,  supply_site_codename %>
 </div>

 <div class="field">
   <%= f.label :demand_site_id %>
   <br/>
    <%= f.collection_select :demand_site_id, DemandSite.find(:all), :id, :demand_site_codename %>
 </div>

现在,它按预期运作。 关键是要确定一种参照相应网站模式的方法。

Stefan





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签