Rails 4 RoutingError: No Route Matches [POST]

While learning Rails 4, I do a little exercise, but when I try to update an object, I make a routing error. I keep getting the error message: There are no matches in the [POST] route "/ movies / 1 / edit" , but I can’t see where my code is wrong:

my movies_controller.rb

class MoviesController < ApplicationController def index @movies = Movie.all end def show @movie = Movie.find(params[:id]) end def new @movie = Movie.new end def create @movie = Movie.create(movie_params) if @movie.save redirect_to "/movies/#{@movie.id}", :notice => "Your movie was saved!" else render "new" end end def edit @movie = Movie.find(params[:id]) end def update @movie = Movie.find(params[:id]) if @movie.update_attributes(params[:movie]) redirect_to "/movies" else render "edit" end end def destroy end private def movie_params params.require(:movie).permit(:name, :genre, :year) end end 

Here is my edit.html.erb

 <h1>Now Editing:</h1> <h3><%= @movie.name %></h3> <%= form_for @movie.name do |f| %> <%= f.label :name %> <%= f.text_field :name %> <br> <%= f.label :genre %> <%= f.text_field :genre %> <br> <%= f.label :year %> <%= f.number_field :year %> <br> <%= f.submit "Update" %> 

and routes.rb file:

 MovieApp::Application.routes.draw do get "movies" => "movies#index" post "movies" => "movies#create" get "movies/new" => "movies#new" get "movies/:id" => "movies#show" get "movies/:id/edit" => "movies#edit" put "movies/:id" => "movies#update" end 

last, here is the output from rake routes :

  Prefix Verb URI Pattern Controller#Action movies GET /movies(.:format) movies#index POST /movies(.:format) movies#create movies_new GET /movies/new(.:format) movies#new GET /movies/:id(.:format) movies#show GET /movies/:id/edit(.:format) movies#edit PUT /movies/:id(.:format) movies#update 
+6
source share
2 answers

form_for @movie.name should be form_for @movie . I can’t say what is happening, but I suspect that it somehow gives you <form action=""> .

+3
source

Your error message indicates that you are sending an email request to the edit URL.

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

While on the route you indicated a request for receipt.

get "movies /: id / edit" => "movies # edit"

I believe this is somehow causing a problem, and so you can change the publication request.

 post "movies/:id/edit" => "movies#edit" 
+2
source

Source: https://habr.com/ru/post/956432/


All Articles