[RAILS] Verwendung von fields_for

Geben Sie im Eltern-Kind-Beziehungsmodell den Fremdschlüssel in der Migrationsdatei des untergeordneten Modells an.

question.rb


#Übergeordnetes Modell
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: 'Erstellt ein Wort'
    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: 'Das Wort bearbeitet'
    else
      render 'edit'
    end
  end

  def destroy
    @question = Question.find(params[:id])
    if @question.destroy
      redirect_to questions_path, notice: 'Das Wort gelöscht'
    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>Wortschöpfung</h1>
<%= form_for @question do |f| %>
  <%= render 'layouts/error_messages', model: f.object %>
  <div class="form-group">
    <%= f.label :question, 'Wort' %>
    <%= f.text_field :question, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :description, 'Erläuterung' %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <%= f.fields_for :question_similars do |q| %>
    <div class="form-group">
      <%= q.label :similar_word, 'Synonyme' %>
      <%= q.text_field :similar_word, class: "form-control" %>
    </div>
  <% end %>
  <%= f.submit 'Erstellen', 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, 'Wort' %>
    <%= f.text_field :question, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :description, 'Erläuterung' %>
    <%= f.text_area :description, class: "form-control" %>
  </div>
  <%= f.fields_for :question_similars do |q| %>
    <div class="form-group">
      <%= q.label :similar_word, 'Synonyme' %>
      <%= q.text_field :similar_word, class: "form-control" %>
      <%= q.hidden_field :id, value: q.object.id %>
    </div>
  <% end %>
  <%= f.submit 'Aktualisieren', class: 'btn btn-primary form-control' %>
<% end %>

Recommended Posts

Verwendung von fields_for
Wie benutzt man rbenv?
Verwendung mit_option
Verwendung von java.util.logging
Verwendung der Karte
Verwendung von collection_select
Wie benutzt man Twitter4J
Wie benutzt man active_hash! !!
Verwendung von MapStruct
Verwendung von TreeSet
[Verwendung des Etiketts]
Wie man Identität benutzt
Wie man Hash benutzt
Verwendung von Dozer.mapper
Wie benutzt man Gradle?
Verwendung von org.immutables
Verwendung von java.util.stream.Collector
Verwendung von VisualVM
Verwendung von Map
Verwendung der Ketten-API
[Java] Verwendung von Map
Verwendung der Warteschlange mit Priorität
[Rails] Verwendung von Enum
Verwendung von Java Optional
Verwendung von JUnit (Anfänger)
Verwendung von Ruby return
[Rails] Verwendung von Enum
Verwendung von @Builder (Lombok)
Verwendung der Java-Klasse
Wie man Big Decimal benutzt
[Java] Verwendung von removeAll ()
Verwendung von String [] args
Verwendung von Rails Join
Verwendung von Java Map
Ruby: Wie man Cookies benutzt
Verwendung von abhängigen :: zerstören
Verwendung von Eclipse Debug_Shell
Verwendung von Apache POI
[Rails] Verwendung der Validierung
Verwendung von Java-Variablen
[Rails] So verwenden Sie authenticate_user!
Verwendung von GC Viewer
Wie man Lombok jetzt benutzt
[Erstellen] Verwendung von JUnit
[Schienen] Verwendung von Scope
Verwendung der link_to-Methode
[Rails] Wie man Edelstein "devise" benutzt
Wie man Lombok im Frühling benutzt
Verwendung von StringBurrer und Arrays.toString.
Verwendung des Arrays (persönliches Memorandum)
Verwendung von HttpClient (Get) von Java