Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 以多对多的方式限制Formtastic收藏_Ruby On Rails 4_Has Many Through_Formtastic - Fatal编程技术网

Ruby on rails 4 以多对多的方式限制Formtastic收藏

Ruby on rails 4 以多对多的方式限制Formtastic收藏,ruby-on-rails-4,has-many-through,formtastic,Ruby On Rails 4,Has Many Through,Formtastic,我已经成功地将formtastic集成到我的rails项目中,并使用它以多对多关系进行多值选择 问题是,我似乎找不到如何将选择限制在相关的范围内 鉴于以下情况: class Pool < ActiveRecord::Base has_many :poolings, dependent: :destroy has_many :commercials, :through => :poolings accepts_nested_attributes_for :poolings ac

我已经成功地将formtastic集成到我的rails项目中,并使用它以多对多关系进行多值选择

问题是,我似乎找不到如何将选择限制在相关的范围内

鉴于以下情况:

class Pool < ActiveRecord::Base
 has_many :poolings, dependent: :destroy
 has_many :commercials, :through => :poolings
 accepts_nested_attributes_for :poolings
 accepts_nested_attributes_for :commercials
 belongs_to :client
end
类池:pool
接受池的\u嵌套\u属性\u
接受\u嵌套的\u属性\u用于:商业广告
属于:客户
结束
多对多模型的关系

class Pooling < ActiveRecord::Base
 belongs_to :pool
 belongs_to :commercial
end
类池
以及商业模式

class Commercial < ActiveRecord::Base
 belongs_to :client
 has_many :poolings, dependent: :destroy
 has_many :pool, :through => :poolings
end
class商业:池
结束
以下代码用于创建多个选择表单:

<%= semantic_form_for @pool do |f| %>
  <%= f.inputs %>
  <%= f.inputs :commercials %>
  <%= f.actions %>
<% end %>

关键在于formtastic的假设:商业广告转化为多对多选择形式

现在阅读文档时,我不明白要添加什么内容

<%= f.inputs :commercials %>

因此,当我在表单中使用它来编辑“client_id=1”的“pool”时,它将只显示带有“client_id=1”的“commercials”

将以下选项与collection select一起使用可获得预期结果:

<%= f.collection_select :commercials, Commercial.where(:client_id => @pool.client), :id, :name %>
@pool.client),:id,:name%>
formtastic中的等价物是什么

顺便说一句,作为RoR的新手,我发现各种组件和gem的工作方式似乎都略有不同,这让我非常困惑。开源的乐趣!

有一个用于选择、复选框和收音机的“集合”参数(请参阅)

对于您的特定问题,解决方案可能如下所示:

<%= semantic_form_for @pool do |f| %>
  <%= f.inputs do %>
    <%= f.input :commercials, :collection => Commercial.where(:client_id => @pool.client) %>
  <% end %>
  <%= f.actions %>
<% end %>

商业。其中(:client_id=>@pool.client)%>