Ruby on Rails Routes

Asked

Viewed 84 times

0

Guys I’m starting at Ruby on Rails and I had a problem with the calls 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

Those were the lines I changed in the code that generated me to url:maquinas/:maquinas_id/verificacaos

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

Someone could give me a light?

Edit: Machine Contoler

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 Verificação

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
  • You can post the controllers code as well?

  • I didn’t alter the controllers, my research says they’re the ones I need to change, but I don’t know how

  • If any form are there

  • You want to see a specific check of a specific machine, correct?

  • Yes, that’s right, for example, when I type machines/:machines

1 answer

0


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

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

    render json: @verificacaos
end

You just 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.