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