ruby-on-rails - Rails 4 路由错误 : No Route Matches [POST]

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

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

我的电影_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

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

最佳答案

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

关于ruby-on-rails - Rails 4 路由错误 : No Route Matches [POST],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19501374/

相关文章:

ruby - 如何动态地向 SitePrism 页面对象添加一个部分?

ruby-on-rails - rails 安装错误

ruby-on-rails - Rails 路线和站点性能

ruby-on-rails - 单个测试的 Rails/RSpec 切换缓存

ruby-on-rails - 将 View 拆分为单独页面的 Rails 方法是什么?

ruby-on-rails - 在 Ruby on Rails 模型对象上执行 map 方法

ruby-on-rails - 使用 Ruby 收集超过 1 个变量

ruby-on-rails - Ruby on Rails 3 : How to retrieve POST and GET params separately?

javascript - Angular : Keep view state between differents views with differents controllers

asp.net-mvc - asp.net mvc 通过 https ssl 路由