Rails 6 嵌套不显示 form_with
Rails 6 nested not displaying form_with
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
workspace
和 customerType
之间的创建完成得很好我在控制器中的输入显示 class
我知道可能有 post 的解决方案,但我找不到它
它没有出现在表格中是否有错误?
您的表单缩进不正确:
它应该看起来像
# _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 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
workspace
和 customerType
之间的创建完成得很好我在控制器中的输入显示 class
我知道可能有 post 的解决方案,但我找不到它
它没有出现在表格中是否有错误?
您的表单缩进不正确:
它应该看起来像
# _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 %>