[RAILS] Comment utiliser fields_for

Dans le modèle de relation parent-enfant, spécifiez la clé étrangère dans le fichier de migration du modèle enfant.

question.rb


#Modèle parent
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: 'Créé un mot'
    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: 'Edité le mot'
    else
      render 'edit'
    end
  end

  def destroy
    @question = Question.find(params[:id])
    if @question.destroy
      redirect_to questions_path, notice: 'Supprimé le mot'
    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>Création de mots</h1>
<%= form_for @question do |f| %>
  <%= render 'layouts/error_messages', model: f.object %>
  <div class="form-group">
    <%= f.label :question, 'mot' %>
    <%= f.text_field :question, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :description, 'Explication' %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <%= f.fields_for :question_similars do |q| %>
    <div class="form-group">
      <%= q.label :similar_word, 'Synonymes' %>
      <%= q.text_field :similar_word, class: "form-control" %>
    </div>
  <% end %>
  <%= f.submit 'Créer', 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, 'mot' %>
    <%= f.text_field :question, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :description, 'Explication' %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <%= f.fields_for :question_similars do |q| %>
    <div class="form-group">
      <%= q.label :similar_word, 'Synonymes' %>
      <%= q.text_field :similar_word, class: "form-control" %>
      <%= q.hidden_field :id, value: q.object.id %>
    </div>
  <% end %>
  <%= f.submit 'Mise à jour', class: 'btn btn-primary form-control' %>
<% end %>

Recommended Posts

Comment utiliser fields_for
Comment utiliser rbenv
Comment utiliser with_option
Comment utiliser java.util.logging
Comment utiliser la carte
Comment utiliser collection_select
Comment utiliser Twitter4J
Comment utiliser active_hash! !!
Comment utiliser MapStruct
Comment utiliser TreeSet
[Comment utiliser l'étiquette]
Comment utiliser l'identité
Comment utiliser le hachage
Comment utiliser Dozer.mapper
Comment utiliser Gradle
Comment utiliser org.immutables
Comment utiliser java.util.stream.Collector
Comment utiliser VisualVM
Comment utiliser Map
Comment utiliser l'API Chain
[Java] Comment utiliser Map
Comment utiliser Queue avec priorité
[Rails] Comment utiliser enum
Comment utiliser java Facultatif
Comment utiliser JUnit (débutant)
Comment utiliser le retour Ruby
[Rails] Comment utiliser enum
Comment utiliser @Builder (Lombok)
Comment utiliser la classe Java
Comment utiliser Big Decimal
[Java] Comment utiliser removeAll ()
Comment utiliser String [] args
Comment utiliser la jonction de rails
Comment utiliser Java Map
Ruby: Comment utiliser les cookies
Comment utiliser Dependant :: Destroy
Comment utiliser Eclipse Debug_Shell
Comment utiliser Apache POI
[Rails] Comment utiliser la validation
Comment utiliser les variables Java
[Rails] Comment utiliser authenticate_user!
Comment utiliser GC Viewer
Comment utiliser Lombok maintenant
[Création] Comment utiliser JUnit
[Rails] Comment utiliser Scope
Comment utiliser la méthode link_to
[Rails] Comment utiliser la "devise" des gemmes
Comment utiliser Lombok au printemps
Comment utiliser StringBurrer et Arrays.toString.
Comment utiliser le tableau (mémorandum personnel)
Comment utiliser HttpClient de Java (Get)