Error editing record in DB

0

Hello, I'm having problems with editar a record:

Follow View

_form.html.erb

<%= form_with(model: cliente, local: true) do |form| %>
  <% if cliente.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(cliente.errors.count, "error") %> prohibited this cliente from being saved:</h2>

      <ul>
      <% cliente.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :cnpj %>
    <%= form.text_field :cnpj, id: :cliente_cnpj %>
  </div>

  <div class="field">
    <%= form.label :razao_social %>
    <%= form.text_field :razao_social, id: :cliente_razao_social %>
  </div>

  <div class="field">
    <%= form.label :num_funcionarios %>
    <%= form.number_field :num_funcionarios, id: :cliente_num_funcionarios %>
  </div>

  <div class="field">
    <%= form.label :processo_id %>
    <%= collection_select(:cliente, :processo_id, @processo_options_for_select, :id, :descricao) %>
  </div>

  <div class="field">
    <%= form.label :status %>
    <%= form.number_field :status, id: :cliente_status, value: '1', :readonly => true%>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

Follow Controller

clients_controller.erb

class ClientesController < ApplicationController
  before_action :set_cliente, only: %i[show edit update destroy]

  # GET /clientes
  # GET /clientes.json
  def index
    @clientes = Cliente.all
  end

  # GET /clientes/1
  # GET /clientes/1.json
  def show; end

  # GET /clientes/new
  def new
    @cliente = Cliente.new
    options_for_select
  end

  # GET /clientes/1/edit
  def edit; end

  # POST /clientes
  # POST /clientes.json
  def create
    @cliente = Cliente.new(cliente_params)

    respond_to do |format|
      if @cliente.save
        format.html { redirect_to @cliente, notice: 'Cliente foi Criado com Sucesso.' }
        format.json { render :show, status: :created, location: @cliente }
      else
        format.html { render :new }
        format.json { render json: @cliente.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /clientes/1
  # PATCH/PUT /clientes/1.json
  def update
    respond_to do |format|
      if @cliente.update(cliente_params)
        format.html { redirect_to @cliente, notice: 'Cliente foi Atualizado com Sucesso.' }
        format.json { render :show, status: :ok, location: @cliente }
      else
        format.html { render :edit }
        format.json { render json: @cliente.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /clientes/1
  # DELETE /clientes/1.json
  def destroy
    destroyReg
    respond_to do |format|
      format.html { redirect_to clientes_url, notice: 'Cliente foi apagado do sistema' }
      format.json { head :no_content }
    end
  end

  private

  def options_for_select
    @processo_options_for_select = Processo.where(status: 1)
  end

  # Use callbacks to share common setup or constraints between actions.
  def set_cliente
    @cliente = Cliente.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def cliente_params
    params.require(:cliente).permit(:cnpj, :razao_social, :num_funcionarios, :processo_id, :status)
  end

  def destroyReg
    conn = ActiveRecord::Base.connection
    if conn.execute("UPDATE Clientes SET status = 0 WHERE ID = #{@cliente.id}")
      puts"Apagado"
    else
      puts"Não Apagou"
    end
  end
end

Follow Exit

Started GET "/clientes" for 127.0.0.1 at 2018-03-28 16:52:19 -0300
Processing by ClientesController#index as HTML
  Rendering clientes/index.html.erb within layouts/application
  Cliente Load (0.4ms)  SELECT "clientes".* FROM "clientes"
  Processo Load (0.3ms)  SELECT  "processos".* FROM "processos" WHERE "processos"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
  Rendered clientes/index.html.erb within layouts/application (9.4ms)
Completed 200 OK in 113ms (Views: 109.4ms | ActiveRecord: 0.7ms)


Started GET "/clientes/18/edit" for 127.0.0.1 at 2018-03-28 16:52:23 -0300
Processing by ClientesController#edit as HTML
  Parameters: {"id"=>"18"}
  Cliente Load (0.2ms)  SELECT  "clientes".* FROM "clientes" WHERE "clientes"."id" = ? LIMIT ?  [["id", 18], ["LIMIT", 1]]
  Rendering clientes/edit.html.erb within layouts/application
  Rendered clientes/_form.html.erb (10.2ms)
  Rendered clientes/edit.html.erb within layouts/application (12.4ms)
Completed 500 Internal Server Error in 20ms (ActiveRecord: 0.2ms)



ActionView::Template::Error (undefined method 'map' for nil:NilClass):
    28: 
    29:   <div class="field">
    30:     <%= form.label :processo_id %>
    31:     <%= collection_select(:cliente, :processo_id, @processo_options_for_select, :id, :descricao) %>
    32:   </div>
    33: 
    34:   <div class="field">

app/views/clientes/_form.html.erb:31:in 'block in _app_views_clientes__form_html_erb___2257324227783179598_69886199691100'
app/views/clientes/_form.html.erb:1:in '_app_views_clientes__form_html_erb___2257324227783179598_69886199691100'
app/views/clientes/edit.html.erb:3:in '_app_views_clientes_edit_html_erb__1150078820149850562_69886199750960'

On the page, he accuses the error as being in line 31 of View

<%= collection_select(:cliente, :processo_id, @processo_options_for_select, :id, :descricao) %>

The point is that to insert a contact, this same error does not happen. There is a method in the controler:

def options_for_select
     @processo_options_for_select = Processo.where(status: 1)
end

What passes the parameter, but I only managed to do it using the where function, because it is a business rule (I know this should be in the model, I'll move on to it in the future). If anyone can give me a light! : D

    
asked by anonymous 28.03.2018 / 21:59

1 answer

0

Fixed problem:

def edit 
    options_for_select
end

The function has been added to the edit method. Allowing the system to use the variable as soon as the action is requested.

    
29.03.2018 / 02:26