【发布时间】:2015-10-10 09:31:36
【问题描述】:
我在 Rails 中使用 SimpleForm,我向用户显示了 2 个字段,用于从预先过滤的一组位置中选择一个位置(丰富的关联)。每个选择都需要连同它所在的位置类型一起保存在连接表中。
如何创建一个表单,将信息发送回我的控制器,告诉我正在选择什么“类型”的位置。
_new_flow_form.html
<%= simple_form_for @new_flow,:url => {:action => "company_create_flow", :controller => 'flows'}, :html => { :class => 'form-horizontal', :multipart => true } do |f| %>
<%= f.label 'Workflow Title', :class => 'control-label' %>
<%= f.text_field :title, :class => 'form-control' %>
<%= f.association :locations, collection: Location.where(:company_id => @company.id, :source => 'TRUE'), :label => 'Source Location' %>
<%= f.association :locations, collection: Location.where(:company_id => @company.id, :destination => 'TRUE'), :label => 'Destinations'%>
<%= f.submit nil, :class => 'btn btn-primary' %>
<% end %>
作为参考,我的模型如下所示:
位置.rb
class Flow < ActiveRecord::Base
has_many :flow_locations
has_many :locations, :through => :flow_locations
end
流.rb
class Location < ActiveRecord::Base
has_many :flow_locations
has_many :flows, :through => :flow_locations
end
flow_location.rb
class FlowLocation < ActiveRecord::Base
belongs_to :flow
belongs_to :location
serialize :source
serialize :destination
end
我在提交表单时得到的当前结果是这样的:
{
"utf8": "✓",
"authenticity_token": "Z3f4lByIebmvz4wgFGQGPP7xoVeLIFgSE0MSak9KfZxP3yoH7SFBjH5upD+mx+pRZ61bXqwl+e2o3qEX3dfYAw==",
"flow": {
"title": "testMe",
"company_id": "1",
"service_ids": [
"",
"2"
],
"location_ids": [
"",
"3",
"",
"1"
]
},
"commit": "Create Flow"
}
这里的主要问题是,我无法从中破译哪些 location_id 用于“来源”,哪些用于“目的地”
谁能帮我理解我在这里可以做些什么不同的事情,以便从可以具体告诉我哪个 location_id 用于哪个视图的视图中得到更好的东西?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 model-associations