How to use fields_for

In the parent-child relationship model, specify foreign_key in the migration file of the child model.

question.rb


#Parent model
class Question < ApplicationRecord
  has_many :question_similars, dependent: :destroy
  accepts_nested_attributes_for :question_similars
  validates :question, presence: true
  validates :description, presence: true
end

question_similar.rb


class QuestionSimilar < ApplicationRecord
  belongs_to :question
  validates :similar_word, presence: true
end

questions_controller.rb


class QuestionsController < ApplicationController
  before_action :require_login
  before_action :session_number_to_zero

  def search
    @questions = Question.where('question ilike ?', "%#{params[:search]}%")
  end

  def index
    @questions = Question.all
  end

  def new
    @question = Question.new
    @question.question_similars.build
  end

  def create
    @question = Question.new(question_params)
    if @question.save
      redirect_to questions_path, notice: 'Created a word'
    else
      render 'new'
    end
  end

  def show
    @question = Question.find(params[:id])
  end

  def edit
    @question = Question.find(params[:id])
  end

  def update
    @question = Question.find(params[:id])
    if @question.update(question_update_params)
      redirect_to questions_path, notice: 'Edited the word'
    else
      render 'edit'
    end
  end

  def destroy
    @question = Question.find(params[:id])
    if @question.destroy
      redirect_to questions_path, notice: 'Deleted the word'
    else
      redirect_to root_url
    end
  end

  private

  def question_params
    params.require(:question).permit(:question, :description, question_similars_attributes:
      [:similar_word, :question_id])
  end

  def question_update_params
    params.require(:question).permit(:question, :description, question_similars_attributes:
      [:similar_word, :question_id, :_delete, :id])
  end
end

questions/new.html.erb


<h1>Word creation</h1>
<%= form_for @question do |f| %>
  <%= render 'layouts/error_messages', model: f.object %>
  <div class="form-group">
    <%= f.label :question, 'word' %>
    <%= f.text_field :question, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :description, 'Explanation' %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <%= f.fields_for :question_similars do |q| %>
    <div class="form-group">
      <%= q.label :similar_word, 'Synonyms' %>
      <%= q.text_field :similar_word, class: "form-control" %>
    </div>
  <% end %>
  <%= f.submit 'Create', class: 'btn btn-primary form-control' %>
<% end %>

questions/edit.html.erb


<%= form_for(@question, url: { controller: 'questions', action: 'update' }) do |f| %>
  <%= render 'layouts/error_messages', model: f.object %>
  <div class="form-group">
    <%= f.label :question, 'word' %>
    <%= f.text_field :question, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :description, 'Explanation' %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <%= f.fields_for :question_similars do |q| %>
    <div class="form-group">
      <%= q.label :similar_word, 'Synonyms' %>
      <%= q.text_field :similar_word, class: "form-control" %>
      <%= q.hidden_field :id, value: q.object.id %>
    </div>
  <% end %>
  <%= f.submit 'Update', class: 'btn btn-primary form-control' %>
<% end %>

Recommended Posts

How to use fields_for
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)