While adding features to a class project, I was struggling with an error that took me some time to debug. This post is meant as a reminder to myself (and possibly others) that ROUTE ORDER IN RAILS MATTERS.
Originally, I had the following code:
get("/movies", { :controller => "movies", :action => "index"})
get("/movies/:id", { :controller => "movies", :action => "movie"})
get("/delete_movie/:id", { :controller => "movies", :action => "delete"})
get("/movies/new", {:controller => "movies", :action => "new_movie"})
The problem that I had initially overlooked was that since Rails compiles the code from top to bottom, it was sending me to the "/movies/:id" when I was trying to use the "/movies/new" path, thinking that "new" was the movie's id. In the program, it was trying to look up a movie with the id of "new", which it would never find, and ended up giving me an error.
Eventually, after multiple changes and continually hitting the error, I finally realized the problem was the route order. Rearranging the routes in the following way, everything worked:
get("/movies", { :controller => "movies", :action => "index"})
get("/movies/new", {:controller => "movies", :action => "new_movie"})
get("/movies/:id", { :controller => "movies", :action => "movie"})
get("/delete_movie/:id", { :controller => "movies", :action => "delete"})
Top comments (0)