ruby - How can I pass extra details from simpleForm elements to my controller in rails -
i'm using simpleform in rails , have 2 fields presented user choose location (a rich association) pre-filtered set of locations. each choice needs saved in join table along type of location is.
how can create form send info controller can tell me 'type' of location being selected.
_new_flow_form.html <%= simple_form_for @new_flow,:url => {:action => "company_create_flow", :controller => 'flows'}, :html => { :class => 'form-horizontal', :multipart => true } |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 %> for reference, models this:
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 flow_location.rb
class flowlocation < activerecord::base belongs_to :flow belongs_to :location serialize :source serialize :destination end the current result when form submitted this:
{ "utf8": "✓", "authenticity_token": "z3f4lbyiebmvz4wgfgqgpp7xovelifgse0msak9kfzxp3yoh7sfbjh5upd+mx+prz61bxqwl+e2o3qex3dfyaw==", "flow": { "title": "testme", "company_id": "1", "service_ids": [ "", "2" ], "location_ids": [ "", "3", "", "1" ] }, "commit": "create flow" } and major problem here that, have no way of deciphering of location_ids 'sources' vs ones 'destinations'
can me understand might able different here better view can tell me location_id which?
you can change name attribute of html elements:
<%= 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"%> but push attribute out of flow object
{ "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" ] } or
in rails 4 can use scoped association in flow model:
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 and update view _new_flow_form.html accordingly:
<%= 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'%>
Comments
Post a Comment