【问题标题】:How can I pass extra details from simpleForm elements to my controller in rails如何将 simpleForm 元素中的额外详细信息传递给 rails 中的控制器
【发布时间】: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


    【解决方案1】:

    您可以只更改 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'%>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多