【问题标题】:Rails 6 nested not displaying form_withRails 6嵌套不显示form_with
【发布时间】:2021-12-06 19:05:18
【问题描述】:
class CustomerType < ApplicationRecord
  belongs_to :workspace, inverse_of: :customer_type
  validates_presence_of :workspace
end

class Workspace < ApplicationRecord
  validates :name, presence: true, uniqueness: { case_sensitive: false }

  has_one :customer_type
  accepts_nested_attributes_for :customer_type, allow_destroy: true
end
# controller

  def new
    @workspace = Workspace.new
    @workspace.build_customer_type
  end
# _form
<%= form_with(model: [:back_office, @workspace]) do |form| %>
...
  <%= form.fields_for :customer_type, @workspace.customer_type do |s| %>
    <%= s.label :build, 'Build', class: 'form-check-label'%>
    <%= s.radio_button :build, 'build', class: 'form-check-input'%>
  <% end %>
...
<% end %>


class CreateCustomerTypes < ActiveRecord::Migration[6.1]
  def change
    create_table :customer_types, id: :uuid do |t|
      t.boolean :build, default: false
      t.boolean :grow, default: false
      t.boolean :connector, default: false
      t.references :workspace, null: false, foreign_key: true, type: :uuid

      t.timestamps
    end
  end
end

workspacecustomerType 之间的创建做得很好,我在控制器中的放置向我展示了类

我知道可能有解决方案的帖子,但我找不到它

它没有出现在表单中是不是有错误?

【问题讨论】:

  • 您可能需要在 Workspace.new 之后的新操作中调用 @workspace.custom_type.build
  • 它做的完全一样,显示器上什么也没有。我认为应该在 form_with 中添加,但我没有语法
  • 尝试这种方式:= f.fields_for :answers, @survey.answers do |answer_form| 构建到位
  • 没有那么奇怪。我需要向您展示更多代码吗?
  • 好的最后一件事我注意到在你提供的代码中 没有缩进,但它应该是。不知道这是否只是复制的错误。

标签: ruby-on-rails ruby form-for nested-form-for form-with


【解决方案1】:

您的表单没有正确缩进:

应该是这样的

# _form
<%= form_with(model: [:back_office, @workspace]) do |form| %>
...
  <%= form.fields_for :customer_type, @workspace.customer_type do |s| %>
    <%= s.label :build, 'Build', class: 'form-check-label'%>
    <%= s.radio_button :build, 'build', class: 'form-check-input'%>
  <% end %>
...
<% end %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多