如何在 rails 中将额外的详细信息从 simpleForm 元素传递到我的控制器
How can I pass extra details from simpleForm elements to my controller in rails
我在 Rails 中使用 SimpleForm,我向用户显示了 2 个字段,用于从一组预先过滤的位置中选择一个位置(丰富的关联)。每个选择都需要连同它所在的位置类型一起保存在连接 table 中。
我如何创建一个表单,将信息发回我的控制器,告诉我正在选择哪个 'type' 位置。
_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 %>
作为参考,我的模型如下所示:
location.rb
class Flow < ActiveRecord::Base
has_many :flow_locations
has_many :locations, :through => :flow_locations
end
flow.rb
class Location < ActiveRecord::Base
has_many :flow_locations
has_many :flows, :through => :flow_locations
end
流量_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_ids 用于 'Sources' 哪些用于 'Destinations'
任何人都可以帮助我了解我可以在这里做些什么不同的事情,以便从可以具体告诉我哪个 location_id 是哪个的视图中得到更好的东西吗?
您只需更改 HTML 元素的 name
属性即可:
<%= f.association :locations, collection: Location.where(:company_id => @company.id, :source => 'TRUE'), :label => 'Source Location' name: "source_ids" %>
<%= f.association :locations, collection: Location.where(:company_id => @company.id, :destination => 'TRUE'), :label => 'Destinations' name: "destination_ids"%>
但这会将属性推出 flow
对象
{
"utf8": "✓",
"authenticity_token": "Z3f4lByIebmvz4wgFGQGPP7xoVeLIFgSE0MSak9KfZxP3yoH7SFBjH5upD+mx+pRZ61bXqwl+e2o3qEX3dfYAw==",
"flow": {
"title": "testMe",
"company_id": "1",
"service_ids": [
"",
"2"
] },
"commit": "Create Flow",
"source_ids":[
"1"
],
"destination_ids":[
"3"
]
}
或
在 Rails 4 中,您可以在 Flow
模型中使用范围关联:
class Flow < ActiveRecord::Base
has_many :flow_locations
has_many :destinations, -> { destinations: true }, :class_name => 'Location', :through => :flow_locations
has_many :sources, -> { source: true }, :class_name => 'Location', :through => :flow_locations
has_many :destinations,
end
并相应地更新您的观点 _new_flow_form.html
:
<%= f.association :sources, collection: Location.where(:company_id => @company.id, :source => 'TRUE'), :label => 'Source Location' %>
<%= f.association :destinations, collection: Location.where(:company_id => @company.id, :destination => 'TRUE'), :label => 'Destinations'%>
我在 Rails 中使用 SimpleForm,我向用户显示了 2 个字段,用于从一组预先过滤的位置中选择一个位置(丰富的关联)。每个选择都需要连同它所在的位置类型一起保存在连接 table 中。
我如何创建一个表单,将信息发回我的控制器,告诉我正在选择哪个 'type' 位置。
_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 %>
作为参考,我的模型如下所示:
location.rb
class Flow < ActiveRecord::Base
has_many :flow_locations
has_many :locations, :through => :flow_locations
end
flow.rb
class Location < ActiveRecord::Base
has_many :flow_locations
has_many :flows, :through => :flow_locations
end
流量_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_ids 用于 'Sources' 哪些用于 'Destinations'
任何人都可以帮助我了解我可以在这里做些什么不同的事情,以便从可以具体告诉我哪个 location_id 是哪个的视图中得到更好的东西吗?
您只需更改 HTML 元素的 name
属性即可:
<%= f.association :locations, collection: Location.where(:company_id => @company.id, :source => 'TRUE'), :label => 'Source Location' name: "source_ids" %>
<%= f.association :locations, collection: Location.where(:company_id => @company.id, :destination => 'TRUE'), :label => 'Destinations' name: "destination_ids"%>
但这会将属性推出 flow
对象
{
"utf8": "✓",
"authenticity_token": "Z3f4lByIebmvz4wgFGQGPP7xoVeLIFgSE0MSak9KfZxP3yoH7SFBjH5upD+mx+pRZ61bXqwl+e2o3qEX3dfYAw==",
"flow": {
"title": "testMe",
"company_id": "1",
"service_ids": [
"",
"2"
] },
"commit": "Create Flow",
"source_ids":[
"1"
],
"destination_ids":[
"3"
]
}
或
在 Rails 4 中,您可以在 Flow
模型中使用范围关联:
class Flow < ActiveRecord::Base
has_many :flow_locations
has_many :destinations, -> { destinations: true }, :class_name => 'Location', :through => :flow_locations
has_many :sources, -> { source: true }, :class_name => 'Location', :through => :flow_locations
has_many :destinations,
end
并相应地更新您的观点 _new_flow_form.html
:
<%= f.association :sources, collection: Location.where(:company_id => @company.id, :source => 'TRUE'), :label => 'Source Location' %>
<%= f.association :destinations, collection: Location.where(:company_id => @company.id, :destination => 'TRUE'), :label => 'Destinations'%>