How to eagerly load validated fields

I have these fields in my form:

= f.fields_for :parts do |builder|
  = builder.input :body, label: builder.object.another_relation.description, required: false

In the main model, I have this check:

validates_associated :parts

In the parts model, I have this check:

validates :body, presence: true

It works well. When I leave some part empty and try to create a model, the form displays again with the correct error messages.

But in the logs, I see that the label requests a database for each part, so I will need to download it.

How?

This was my attempt:

= f.fields_for :parts, f.object.parts.includes(:question_part) do |builder|
  = builder.input :body, label: builder.object.another_relation.description, required: false

This desire loads the relationship successfully, but when I leave some field empty and try to create a model, the form is re-displayed with all parts empty and without errors. I think this is normal, as I force fields_for to load fields from this collection that I am passing.

Does anyone know how to solve this?

+4
1

.

4 , -, StackOverflow, , :)

enter image description here

, N , N . N . , .

class Question
  has_many :parts, class_name: 'QuestionPart', dependent: :destroy
  accepts_nested_attributes_for :parts, allow_destroy: true
  has_many :answers, dependent: :destroy

  # ...
end

class QuestionPart
  belongs_to :question

  # ...
end

class Answer
  belongs_to :question
  belongs_to :user
  has_many :parts, class_name: 'AnswerPart', dependent: :destroy
  accepts_nested_attributes_for :parts, allow_destroy: true

  # ...
end

class AnswerPart
  belongs_to :answer
  belongs_to :question_part

  validates :body, presence: true

  # ...
end

:

class AnswersController < ApplicationController
  def new
    @question = Question.find(params[:question_id)
    @answer = @question.answers.new
    @question.parts.each { |p| @answer.parts.build(question_part: p) }
  end

  def edit
    @answer = Answer.find(params[:id])
    @answer_parts = @answer.parts.includes(:question_part)
  end

  def create
    @question = Question.find(params[:question_id])
    @answer = current_user.answers.build(answer_params.merge(question: @question))

    if @answer.save
      redirect_to @question, notice: 'The answer was successfully created.'
    else
      render :new
    end
  end

  def update
    #@answer = Answer.find(params[:id])
    @answer = Answer.where(id: params[:id]).includes(parts: [:question_part]).first

    if @answer.update(answer_params)
      redirect_to @answer.question, notice: 'The answer was successfully updated.'
    else
      render :edit
    end
  end

  private

  def answer_params
    params.require(:answer).permit(:body, parts_attributes: [:id, :question_part_id, :body])
  end
end

, . , ( ). . - , .

:

# new.html.erb
...
<%= form_for [@question, @answer] do |f|
  <%= render 'form', f: f, parts: @answer.parts
<% end %>

# edit.html.erb
...
<%= form_for [@answer] do |f|
  <%= render 'form', f: f, parts: @answer_parts || @answer.parts
<% end %>

# _form.html.erb
...
<%= f.fields_for :parts, parts do |builder| %>
  <%= builder.text_field :body, label: builder.object.question_part.description, required: false
  <%= builder.hidden_field :question_part_id %>  
<% end %>
+3

Source: https://habr.com/ru/post/1547572/


All Articles