0

I am currently creating a movie application and I am confused as to how I should set my fetch up to make a delete request. The way my app works is that the user can create an account, and create movie postings of their favorite movies and they are able to view all the movies that have been created by all users. I want the user to have the ability to delete their own movie posts when they are logged in. I am using react on the front end and rails on the back end

If I use this link... http://localhost:3000/users/${this.state.current_user_id}/movies the current user logged in will be able to view all of their movies that they created. The current_user_id is for the current user that is logged in.

 handleMovieDelete = (movie) => {

        if(this.state.current_user_id){       
   fetch(`http://localhost:3000/users/
   ${this.state.current_user_id}/movies/${movie.id}`, {

         method: 'DELETE'
        })
      .then(res => res.json())
.then(() => {
     this.setState({
         movies: this.state.movies.filter(a_movie => movie.id !==     
         a_movie.id)
      })
    })
  } 
 }

I tried going to http://localhost:3000/users/ ${this.state.current_user_id}/movies/${movie.id} in the browser with the movie id attached at the end and it showed in the rails that there is no route specified, So I know I need to probably do something for the routes but I am not sure how to go about it...

hannah
  • 195
  • 1
  • 12

2 Answers2

0

To create an route you should go to your routes.rb file and do something like this:

get "users/:current_user_id/movies/:movie_id" => "users/movies#show"

Where users/movies references to the movies controller where you will define the action show, wich can be something like that:

def show
   @movie = Movie.find(params[:movie_id])
end

Remember that for that to work you should have an show.html.erb file under your users/movies/ directory

Lucas Vieira
  • 96
  • 2
  • 8
  • This is to delete a single movie? – hannah Sep 22 '19 at 22:41
  • Because i know the destroy method in the controller is a must to delete, i didnt know the show method was also necessary – hannah Sep 22 '19 at 22:42
  • I'm sorry, i was just giving a example of how routing works, should have used delete method for that. In your case, you can do like what i did, but trade the `get` method for `delete` and it should work. – Lucas Vieira Sep 23 '19 at 00:38
0

resources :users do resources :movies end

It gives all urls with user specific movies created.

DELETE /users/:user_id/movies/:id Show /users/:user_id/movies/:id

Padma
  • 1