0
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

The creation between workspace and customerType is done well my puts in the controller shows me the class

I know there is probably a post with the solution but I couldn't find it

it does not appear in the form is there an error?

21
  • you might have to call @workspace.custom_type.build in your new action after Workspace.new Commented Dec 6, 2021 at 20:02
  • it does exactly the same thing, nothing on the display. I think it should be added in the form_with but I don't have the syntax Commented Dec 6, 2021 at 20:08
  • try something along this way: = f.fields_for :answers, @survey.answers do |answer_form| with the build in place Commented Dec 6, 2021 at 20:10
  • <% form.fields_for :customer_type, @workspace.customer_type do |s| %> doesn't work as weird. Do I have to show you more code? Commented Dec 6, 2021 at 20:15
  • 1
    Ok one last thing I noticed that in your presented code <% form.fields_for :customer_type do |s| %> is not indented, but it should be. not sure if it is just a mistake from copying. Commented Dec 6, 2021 at 20:28

1 Answer 1

1

your form was not indented correctly:

it should look like

# _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 %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.