Ruby on rails routes

0

I'm starting in Ruby on Rails and I had a problem with nested routes :

 class Maquina < ApplicationRecord
        has_many :verificacaos
        accepts_nested_attributes_for :verificacaos
    end

    class Verificacao < ApplicationRecord
      belongs_to :maquina
    end

      resources :maquinas do
        resources :verificacaos
      end

These were the lines that I changed in the code that gave me the url:maquinas/:maquinas_id/verificacaos

However, it does not only show the specific check for the requested ip machine, but all machines independent of the computer to which it is related.

Could someone give me a light?

Edit: Contoler Machine

class MaquinasController < ApplicationController
  before_action :set_maquina, only: [:show, :update, :destroy]

  # GET /maquinas
  def index
    @maquinas = Maquina.all

    render json: @maquinas
  end

  # GET /maquinas/1
  def show
    render json: @maquina
  end

  # POST /maquinas
  def create
    @maquina = Maquina.new(maquina_params)

    if @maquina.save
      render json: @maquina, status: :created, location: @maquina
    else
      render json: @maquina.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /maquinas/1
  def update
    if @maquina.update(maquina_params)
      render json: @maquina
    else
      render json: @maquina.errors, status: :unprocessable_entity
    end
  end

  # DELETE /maquinas/1
  def destroy
    @maquina.destroy
  end

  private
    # Use callbacks to share coMon setup or constraints between actions.
    def set_maquina
      @maquina = Maquina.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def maquina_params
      params.require(:maquina).permit(:marca, :modelo, :numeroPatrimonio, :numeroSerie, :nomeMaquina, :usuarioMaquina, :processador, :memoriaRam, :hd, :sistemaOperacional, :setor, :descricao)
    end
end

Controler Verification

class VerificacaosController < ApplicationController
  before_action :set_verificacao, only: [:show, :update, :destroy]

  # GET /verificacaos
  def index
    @verificacaos = Verificacao.all

    render json: @verificacaos
  end

  # GET /verificacaos/1
  def show
    render json: @verificacao
  end

  # POST /verificacaos
  def create
    @verificacao = Verificacao.new(verificacao_params)

    if @verificacao.save
      render json: @verificacao, status: :created, location: @verificacao
    else
      render json: @verificacao.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /verificacaos/1
  def update
    if @verificacao.update(verificacao_params)
      render json: @verificacao
    else
      render json: @verificacao.errors, status: :unprocessable_entity
    end
  end

  # DELETE /verificacaos/1
  def destroy
    @verificacao.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_verificacao
      @verificacao = Verificacao.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def verificacao_params
      params.require(:verificacao).permit(:data, :status, :verificadoPor, :maquina_id, :observacao)
    end
end
    
asked by anonymous 23.01.2018 / 19:46

1 answer

0

Your problem is in VerificacaosController . The maquinas/:maquinas_id/verificacaos is processed by the action index , which is returning everything:

# GET /verificacaos
def index
    @verificacaos = Verificacao.all

    render json: @verificacaos
end

You only need to include :maquinas_id in the query:

# GET /verificacaos
def index
    @verificacaos = Verificacao.where(maquina_id: params[:maquina_id])

    render json: @verificacaos
end

And the action will bring only the relevant objects.

    
25.01.2018 / 16:44