label does not change in Rails 6.0

Asked 2 years ago, Updated 2 years ago, 45 views

I would like to implement it so that I can update records of multiple models in one form, but the label notation is not reflected well.
The model and controller have the following code because we want to store the records in the time_tables table and the records in the artists table at the same time.

class TimeTable <ApplicationRecord
  has_many:schedule# Multiple schedules (intermediate table)
  has_many:artists,through::schedules# Multiple Artists with Schedules in Intermediate Model
  belongs_to — Belongs to the user#User model

  validates_associated —Run validation of the artists#Artist model
  accepts_nested_attributes_for:artists,allow_destroy:true#You can update and delete the Artist model
end
def new
    @time_table=TimeTable.new
    10.times do
      @time_table.artists.build
    end
  end

I didn't know how to register in bulk, so I tried to create a model for the artist table 10 times using times.do.
The erb file is written as follows, but since loop statements are used, there can be more than one form field, but the labels are all the same.

# Excerpt
     <div class="form-row">
       <%=f.fields_for: artists do | a | %>
        <div class='form-group'>
          <%=a.label:name, '10:00'%>
          <%=a.text_field:name,class:'form-control'%>
        </div>
      <%end%>

I would like to create a label that matches each form without looping the label, but I don't know how to implement it, so please let me know.

ruby-on-rails ruby

2022-09-30 19:26

1 Answers

First of all, accepts_nested_attributes_for is a feature provided by Rails, but it is a highly disliked semi-recommended feature.
https://github.com/rails/rails/pull/26976#discussion_r87855694

I'd like to kill acceptance_nested_attributes_for due time.

So

I think it's desirable that

If you could use accepts_nested_attributes_for to achieve this

First of all, accepts_nested_attributes_for is a feature provided by Rails, but it is a highly disliked semi-recommended feature

  • Create a Form Object class to correspond to the data exchanged in the form
  • Use the value sent from the form on the controller to create a new Form Object
  • Save to database via Form Object

I think it's desirable that

If you could use accepts_nested_attributes_for to achieve this

#config/routes.rb
Rails.application.routes.drawdo
  resources 'foos'
end

# app/models/foo.rb
class Foo<ApplicationRecord
  has_many —bar,
           dependent: —destroy
  accepts_nested_attributes_for:bar,
                                allow_destroy —true

  # fields_for to accepts_nested_attributes_for +has_many 
  # necessary to communicate that it is a combinations
  defbars_attributes=(attributes)
  end
end

# app/models/bar.rb
class Bar <ApplicationRecord
  belongs_to: foo,
             optional —true
end

# app/controllers/foos_controller.rb
class FoosController <ApplicationController
  def new
    @foo=Foo.new
    # create a three-coco element
    @f oo.bar.build
    @f oo.bar.build
    @f oo.bar.build
  end

  def create
    params.permit!
    @foo=Foo.new (params[:foo])
    @foo.save!
  end
end
<%#app/views/foos/new.html.erb%>
<%=form_with(model:@foo)do|f|%>
  <ul>
    <%=f.fields_for(:bar,:bar){|f_bar|%>
      <li>index:<%=f_bar.index%>/li>
      <li>name:<%=f_bar.text_field:name%>/li>
    <%}%>
  </ul>
  <%=submit_tag%>
<%end%>


<%#app/views/foos/create.html.erb%>
<%@f oo.bar.each {|bar|%>
  <p><%=bar.name%>/p>
<%}%>

I think it will be like this

To be honest, I wrote this code while reading the source code of fields_for, so it could be broken in any version upgrade.
Accepts_nested_attributes_for should not be used


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.