Veske
Veske

Reputation: 557

Routes error when trying to update a database entry

So I have this database about movies. I have made a resources :movies entry into my routes file for it, which should give it every route needed.

This is my movies controller: https://github.com/Veske/form/blob/ryhm/app/controllers/movies_controller.rb

And this is the error I get when I try to edit a movie on whatever.com/movies/1/edit:

Routing Error
No route matches [POST] "/movies/1/edit"

These are the routes for movies right now:

movies_path  GET     /movies(.:format)   movies#index
POST     /movies(.:format)   movies#create
new_movie_path   GET     /movies/new(.:format)   movies#new
edit_movie_path  GET     /movies/:id/edit(.:format)  movies#edit
movie_path   GET     /movies/:id(.:format)   movies#show
PATCH    /movies/:id(.:format)   movies#update
PUT  /movies/:id(.:format)   movies#update
DELETE   /movies/:id(.:format)   movies#destroy

Now it is clearly seen that there is indeed no POST for movies/:id/edit right now. But why is that? Shouldnt the line I entered to routes give it to me?

EDIT:

Link to movies view: https://github.com/Veske/form/blob/ryhm/app/views/movies/edit.html.erb

Upvotes: 1

Views: 61

Answers (2)

Dhaulagiri
Dhaulagiri

Reputation: 3291

Your MoviesController needs to set the movie in the edit action

def edit
  @movie = Movie.find(params[:id])
end

Or better yet, DRY out your code and create a before_action that will set it for you for your restful routes

before_action :set_movie, only: [:show, :edit, :update, :destroy]

def set_movie
  @movie = Movie.find(params[:id])
end

Upvotes: 1

CDub
CDub

Reputation: 13344

Your problem is that you're calling a POST on the form on the edit page, thus why you're getting the error No route matches [POST] "/movies/1/edit".

Without seeing your code, this is just a guess, but either:

  1. The form will need to be changed to use the PUT method.
  2. The link to get to the edit for is bad, and needs to be a GET request.

Upvotes: 1

Related Questions