vendredi 17 mai 2019

Unable to do a successful delete ajax request in ember

I am trying to make a button delete an item which is an event. In order to do so I have to do an ajax delete request and adjust the event-controller and routes.rb.

This is in my hbs file where I have my button:

<button class="event__delete-btn" >Delete</button>

This is my action remove function in event.js:

actions:{
    remove: function (model) {
        console.log(model.id);
        Ember.$.ajax({
               method: "DELETE",
               url: ":"+model.id
             })
    }
  }

This is my Events Controller I only added the destroy function:

class EventsController < ApplicationController
  def index
    render json: Event.all
  end

  def show
    render json: Event.find_by(slug: params[:slug])
  end

  def destroy
    @event = Event.find(params[:id])
    @event.destroy
    render json: {}
  end

  def create
    event = Event.new(filtered_params)

    if event.save
      render json: event
    else
      render json: { errors: event.errors }, status: 422
    end

  end

  private

  def filtered_params
    params.require(:event).permit(
      :name,
      :description,
      :location,
      :event_date,
      :start_time,
      :end_time,
      :contact
    )
  end
end

These are the routes I have in routes.rb, I added the delete one:

  get 'events' => 'events#index'
  get 'events/:slug' => 'events#show'
  post 'events' => 'events#create'
  delete '/event/:id' => 'events#destroy'

But whenever I am pressing delete I get: DELETE http://localhost:4200/event/:1 404 (Not Found) I am new to ember and rails so what did I do wrong?




Aucun commentaire:

Enregistrer un commentaire