Nested resources with devise problem

1

The client has many quotes and I am trying to delete or edit the quotes according to the client that is connected, but I get this error

  

Could not find Quote with 'id' = 2 [WHERE "quotes". "client_id" = $ 1]

the id that is rescued belongs to the second user but I can not delete the quote he made, I can after creating a quote enter the index but then to edit or delete it throws me the same error and no longer more to do

Routes.rb

devise_for :clients
resources :clients do
    resources :cotizacions
end

Quotation driver

class CotizacionsController < ApplicationController
  before_action :set_cotizacion, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_client!
   # GET /cotizacions
  # GET /cotizacions.json
  def index
    client= Client.find(params[:client_id])
    @cotizacions = client.cotizacions
 end

 # GET /cotizacions/1
 # GET /cotizacions/1.json
 def show
   client = Client.find(params[:client_id])
   @cotizacion = client.cotizacions.find(params[:id])
 end

 # GET /cotizacions/new
 def new
   client = Client.find(params[:client_id])
   @cotizacion = client.cotizacions.build
 end

 # GET /cotizacions/1/edit
 def edit
    client = Client.find(params[:client_id])
    @cotizacion = client.cotizacions.find(params[:id])
 end

 # POST /cotizacions
 # POST /cotizacions.json


 #Asociacion de id cliente con cotizacion
 def create
  client=Client.find(params[:client_id])
  @cotizacion = client.cotizacions.create(cotizacion_params)

  respond_to do |format|
    if @cotizacion.save
      format.html { redirect_to      client_cotizacions_path(params[:client_id]), notice: 'Cotizacion Creada.' }
      format.json { render :show, status: :created, location: @cotizacion }
    else
       format.html { render :new }
       format.json { render json: @cotizacion.errors, status: :unprocessable_entity }
    end
 end
end

 # PATCH/PUT /cotizacions/1
 # PATCH/PUT /cotizacions/1.json
 def update

   client = Client.find(params[:client_id])
   @cotizacion = client.cotizacions.find(params[:id])
     respond_to do |format|
       if @cotizacion.update(cotizacion_params)
         format.html { redirect_to client_cotizacion_path, notice: 'Edicion de cotizacion exitosa.' }
         format.json { render :show, status: :ok, location: @cotizacion }
       else
        format.html { render :edit }
        format.json { render json: @cotizacion.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /cotizacions/1
# DELETE /cotizacions/1.json
def destroy
 @client = Client.find(current_client)
  #2nd you retrieve the comment thanks to params[:id]
  @cotizacion = client.cotizacion.find(params[:id])
  @cotizacion.destroy
  flash[:success] = "Work deleted"
  redirect_to pageclient_paginaclients_path
end


 private
   # Use callbacks to share common setup or constraints between actions.
   def set_cotizacion
     @cotizacion = current_client.cotizacions.find(params[:id])
   end

    # Never trust parameters from the scary internet, only allow the white list through.
   def cotizacion_params
     params.require(:cotizacion).permit(:id,:descripcion, :estado,:category_id, :client_id, :area, :sector, :tiempo, :provincia)
    end


end

Index view.

<h1>Cotizaciones</h1>

 <table>
 <thead>
   <tr>
     <th>Codigo</th>
     <th>Descripcion</th>
     <th>Estado</th>
     <th colspan="3"></th>
  </tr>
 </thead>

 <tbody>
   <% @cotizacions.each do |cotizacion| %>
    <tr>
      <td><%=cotizacion.id %></td>
      <td><%= cotizacion.area %></td>
      <td><%= cotizacion.sector %></td>
      <td><%= cotizacion.estado %></td>

      <td><%= link_to 'Edit', edit_client_cotizacion_path(id:current_client.id) %></td>
      <td><%= link_to 'Destroy',client_cotizacion_path(id:current_client.id),method: :delete, data: { confirm: 'Are you sure?' } %></td>
     </tr>
   <% end %>
  </tbody>
 </table>


Routes

client_cotizacions GET    /clients/:client_id/cotizacions(.:format)                         cotizacions#index
                            POST   /clients/:client_id  /cotizacions(.:format)              cotizacions#create
      new_client_cotizacion GET    /clients/:client_id/cotizacions/new(.:format)          cotizacions#new
     edit_client_cotizacion GET    /clients/:client_id/cotizacions/:id/edit(.:format)     cotizacions#edit
          client_cotizacion GET    /clients/:client_id/cotizacions/:id(.:format)          cotizacions#show
                            PATCH  /clients/:client_id/cotizacions/:id(.:format)          cotizacions#update
                            PUT    /clients/:client_id/cotizacions/:id(.:format)          cotizacions#update
                            DELETE /clients/:client_id/cotizacions/:id(.:format)          cotizacions#destroy
    
asked by mariansa 27.02.2017 в 17:33
source

1 answer

0

When you say:

  

id that is rescued belongs to the second user ...

I think the problem is because you're bringing cotizacions not associated with Client , which you can solve with .includes

cliente = Client.includes(:cotizacions).find(params[:client_id])

I hope it serves you.

    
answered by 27.02.2017 / 20:29
source