Ruby-on-rails Rails 4 路由错误:没有路由匹配 [POST]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19501374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:54:28  来源:igfitidea点击:

Rails 4 RoutingError: No Route Matches [POST]

ruby-on-railsrubyroutesruby-on-rails-4

提问by TomK

I'm working through a small exercise while learning Rails 4, but running into a routing error while trying to update an object. I keep getting an error message: No route matches [POST] "/movies/1/edit"but can't see where my code is not correct:

我在学习 Rails 4 时正在做一个小练习,但是在尝试更新对象时遇到了路由错误。我不断收到错误消息: 没有路由匹配 [POST] "/movies/1/edit"但看不到我的代码在哪里不正确:

my movies_controller.rb

我的电影控制器.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's my edit.html.erb

这是我的 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 the routes.rb file:

和 routes.rb 文件:

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's the output from running rake routes:

最后,这是运行的输出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

采纳答案by meagar

form_for @movie.nameshould be form_for @movie. I can't tell what's going on, but I suspect this is somehow giving you a <form action="">.

form_for @movie.name应该是form_for @movie。我不知道发生了什么,但我怀疑这会以某种方式给你一个<form action="">.

回答by Zero Fiber

Your error message shows that you are sending a post request to the edit url.

您的错误消息显示您正在向编辑网址发送发布请求。

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

没有路由匹配 [POST] "/movies/1/edit"

Whereas in the route you have specified a get request.

而在路由中您指定了一个获取请求。

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

得到“电影/:id/edit”=>“电影#edit”

I believe that is somehow causing the problem and so you could change the request to post.

我相信这以某种方式导致了问题,因此您可以更改要发布的请求。

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

回答by sultan alsamaani

in index file if you are using

在索引文件中,如果您正在使用

button_to 'Edit', edit_movie_path(movie)

change it to

将其更改为

link_to 'Edit', edit_movie_path(movie)

because button send it as POSTbut the link will send it as GET.

因为按钮将其发送为POST但链接将其发送为GET.