dimanche 19 juin 2022

Edit and Update for assosiated model Rails

I tried to create edit and update action for my assosiated model named as Entity. But When edit pages pop up no saved data shown. Means it is showing all field as empty and when I put values in it, creates a another object. And also validation messages are not showing

Entities Controller

class EntitiesController < ApplicationController

  def index

  if params[:search]
    @schema =  Schema.find_by(id: params[:schema_id])
    @entities = @schema.entities.search(params[:search]).order("created_at DESC")
  elsif params[:schema_id]
    @schema =  Schema.find_by(id: params[:schema_id])
    @entities = @schema.entities
  else
    @schema =  Schema.find_by(id: params[:id])
    @entities = @schema.entities
  end
  # binding.pry
end

  def new
      @schema =  Schema.find(params[:schema_id])
      @entity = Entity.new
  end

  def create
    @schema =  Schema.find(params[:schema_id])
    @entity = @schema.entities.new(entity_params)
    if @entity.save
      redirect_to schema_entities_path(@schema)
    else
      redirect_to new_schema_entity_path(@schema)
    end
  end

  def edit
    p '6532646546575675676576575'
    p 'Entering into edit method'
      @schema = Schema.find(params[:schema_id])
      p @schema
      p '6532646546575675676576575'
      @entity = @schema.entities.find(params[:id])
      p @entity
        p '6532646546575675676576575'
  end
  def update
      @schema = Schema.find(params[:schema_id])
      @entity = @schema.entities.find(params[:id])

      if @entity.update(entity_params)
        redirect_to schema_entities_path(@schema)
      else
        render 'edit'
      end

  end

  def destroy
    @schema = Schema.find(params[:schema_id])
    @entity = @schema.entities.find(params[:id])
    @entity.destroy
    redirect_to schema_entities_path(@schema)
  end
  private

    def entity_params
       params.require(:entity).permit(:clientId, :name, :description, :mnemonic)
   end
end

edit form for it:

<%= form_for([@schema, @schema.entities.build], data: { turbo: false }) do |f| %>
      <%= f.text_field :clientId, placeholder:"ClientId" %>
      <%= f.text_area :name, placeholder: "Name" %>
      <%= f.text_area :description, placeholder: "Description" %>
      <%= f.text_area :mnemonic, placeholder: "Mnemonic" %>
      <%= f.submit 'Submit' %>


  <% if @entity.errors.any? %>
     <div id="error_explanation">
         <% @entity.errors.full_messages.each do |msg| %>
           <p class="error_msg"><%= msg %></p>
         <% end %>
     </div>
     <% end %>
<% end %>

Its model:

class Entity < ApplicationRecord
  belongs_to :schema
  has_many :fields, dependent: :destroy
  has_many :joins, through: :fields


  validates :name, presence: true, uniqueness: true

  def self.search(search)
    where("name LIKE ?", "%#{search}%")
  end
end

This is how I am passing values from index page:

<%= link_to 'Edit', edit_schema_entity_path(schema_id: @schema.id, id: data.id)  if data.id %>

Aucun commentaire:

Enregistrer un commentaire