Ruby on rails 4 Rails 4-在嵌套资源中添加现有关联

Ruby on rails 4 Rails 4-在嵌套资源中添加现有关联,ruby-on-rails-4,model-associations,Ruby On Rails 4,Model Associations,我有3种型号: class Resource < ActiveRecord::Base has_many :blocks has_many :topics, :through => :blocks, dependent: :destroy class Topic < ActiveRecord::Base has_many :blocks has_many :resources, :through => :blocks, :class_name =>

我有3种型号:

class Resource < ActiveRecord::Base
  has_many :blocks
  has_many :topics, :through => :blocks, dependent: :destroy

class Topic < ActiveRecord::Base
  has_many :blocks
  has_many :resources, :through => :blocks, :class_name => 'Resource', dependent: :destroy
  accepts_nested_attributes_for :resources
  accepts_nested_attributes_for :blocks, :allow_destroy => true

class Block < ActiveRecord::Base
  belongs_to :resource
  belongs_to :topic
  accepts_nested_attributes_for :resource, :allow_destroy => true, :reject_if => :all_blank
一切正常,除了我希望在添加资源中有一种方法,可以在下拉列表中选择现有资源,还可以创建一个新的正常工作的资源

因此,我想要一个显示所有资源的下拉列表,然后将我选择的资源添加到块模型中。换句话说,我只需要通过块模型在主题和资源之间创建关联

我能做到这一点,当我有一个多选择,但我撞了我的头,让它与一个资源只添加到那些已经选定的可用

我明白了吗/

谢谢!
-文森特

如果你有这样一个复杂的逻辑,最好用点什么 喜欢保存多个模型和模型之间的关系 他们即,将保存逻辑移到两个模型之外

access\u嵌套的\u属性\u应仅用于非常简单的 案例与Rails的大多数神奇功能相似

namespace :admin do
  resources :topics do
    resources :resources do
      post :link
      delete :unlink