ruby-on-rails - 保存时 Rails grouped_collection_select 不起作用

标签 ruby-on-rails ruby-on-rails-5

我正在使用 grouped_collection_select在以 rails 5 形式过滤掉相关信息中。

第一grouped_collection_select 一起使用属性(property)过滤掉与 相关的相关数据合作伙伴 .但是,第二个 grouped_collection_select过滤时有效 字段 关联到 属性(property) ,但在尝试保存时出现错误:

1 error prohibited this trial from being saved:

Field must exist

表格
<%= form_with(model: trial, local: true) do |f| %>
 <label>Co-operator</label>
 <%= f.collection_select :cooperator_id, Cooperator.order('last_name'), :id, :full_name %>
 <label>Property</label>
 <%= f.grouped_collection_select :property_id, Cooperator.order('last_name'), :properties, :full_name, :cooperator_id, :name %>
 <label>Field</label>
 <%= f.grouped_collection_select :field_id, Property.order('name'), :fields, :name, :property_id, :field_name %>
 <%= f.submit 'Submit' %>
<% end %>

当我更改 grouped_collection_selectcollection_select它可以正常工作。但是,这不适合我的需要。
<%= f.collection_select :field_id, Field.all, :id, :field_name %>

审判控制员
def trial_params
 params.require(:trial).permit(:cooperator_id, :field_id, :property_id)
end

试用模型
class Trial < ApplicationRecord
  belongs_to :cooperator
  belongs_to :property
  belongs_to :field
end

日志
Processing by TrialsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"THfy+JGBYbNvzurUscPfP8LQbnnvIz1HBEfeFRiZrocXtiu4ayncEA8cNBA2IkPgcphLoa0QWsEueFBEP29OXA==", "trial"=>{"cooperator_id"=>"2", "property_id"=>"2", "field_id"=>""}, "commit"=>"Create trial", "id"=>"11"}
Cooperator Load (0.5ms)  SELECT  "cooperators".* FROM "cooperators" WHERE "cooperators"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/trials_controller.rb:49
  Property Load (0.4ms)  SELECT  "properties".* FROM "properties" WHERE "properties"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/trials_controller.rb:49
Field Load (0.4ms)  SELECT "fields".* FROM "fields"
  ↳ app/views/trials/_form.html.erb:39
Rendered trials/_form.html.erb (15.3ms)
  Rendered trials/edit.html.erb within layouts/application (16.6ms)
  Rendered partials/_top_nav.html.erb (0.5ms)
  Rendered partials/_main_nav.html.erb (0.8ms)
Completed 200 OK in 63ms (Views: 46.9ms | ActiveRecord: 8.2ms)

最佳答案

表单代码对我来说不正确,第一个分组集合应该是这样的:
<%= f.grouped_collection_select :property_id, Cooperator.order('last_name'), :properties, :full_name, :id, :name %> # 注意 cooperator_id替换为 id因为这需要是应该在选择时设置的值。您的原始代码会将其设置为 Cooperator 的 ID 而不是 Property。

同样,第二个应该是这样的:
<%= f.grouped_collection_select :field_id, Property.order('name'), :fields, :name, :id, :field_name %>

关于ruby-on-rails - 保存时 Rails grouped_collection_select 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52549469/

相关文章:

ruby-on-rails - CarrierWave 与 Cloudfront 生成错误的 URL

ruby-on-rails - 富文本区域被隐藏并且不显示任何内容

mysql - 使用连接选择属性并在 rails 中选择

ruby-on-rails - 如何对所有货币进行分组和求和

ruby-on-rails - 为什么需要将 JWT 作为 Bearer Token header 发送?

ruby-on-rails - elm 将 json 发布到 Rails

ruby-on-rails-5 - Assets 未声明为在生产中预编译(Rails 5.0.0.1)

javascript - 防止 turbolinks 5 在某些 <a></a> 上加载

ruby-on-rails - 每次 Controller 或模型更改时,Rails 5 都会重新启动服务器

javascript - Rails - 我如何确保部分已在 js.erb 中呈现