【问题标题】:Rails 4 RoutingError: No Route Matches [POST]Rails 4 RoutingError:没有路由匹配 [POST]
【发布时间】:2013-10-30 08:23:35
【问题描述】:

我在学习 Rails 4 时正在做一个小练习,但在尝试更新对象时遇到了路由错误。我不断收到一条错误消息:No route matches [POST] "/movies/1/edit" 但看不到我的代码不正确的地方:

我的电影控制器.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

这是我的 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" %>    

还有 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

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

【问题讨论】:

    标签: ruby-on-rails ruby routes ruby-on-rails-4


    【解决方案1】:

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

    【讨论】:

    • 这也是我的猜测,空白操作将返回到 current_url,即编辑路径
    • 谢谢@meager,但我现在收到了NoMethodError in Movies#edit: undefined method `movie_path' for #&lt;#&lt;Class:0x007ff4cb263e38&gt;:0x007ff4c99f6300&gt;
    • 您尚未命名路线。您需要使用get "movies" =&gt; "movies#index", as: "movies",或者,正确的做法是丢弃所有路线并使用resources :movies
    • 没有命名路线是问题,@meager,谢谢。我知道我可以将resources :movies 放入我的routes.rb 文件中,但我想手动完成以了解路由的工作原理。再次感谢~
    【解决方案2】:

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

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

    而在路由中你已经指定了一个获取请求。

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

    我认为这是导致问题的原因,因此您可以更改发布请求。

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

    【讨论】:

    • 这不是真正的问题。你supposed到'GET'编辑表单,并且POST/PUT它返回资源以进行更改。将编辑更改为 POST 会破坏这一点。这不是正确的做法。
    • 他的路线很好,他绝对不应该修改他的路线以接受错误的方法。这只会给他带来另一个错误,他的路线肯定永远不会到达它需要到达的movies#update
    【解决方案3】:

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

    button_to 'Edit', edit_movie_path(movie)
    

    改成

    link_to 'Edit', edit_movie_path(movie)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多