【问题标题】:"The page you were looking for doesn't exist." edit form“您要查找的页面不存在。”编辑表格
【发布时间】:2015-01-14 06:45:21
【问题描述】:

我有以下路线

namespace :dashboard do
  get   '/courses/:id/edit'                     => 'courses#edit',                :as => :edit_course
  put   'courses/:id/update'                    => 'courses#update'
end

还有这个表格

  = form_tag dashboard_edit_course_url( @course), :method => 'post', :multipart => true do
    ...

动作是:

<form accept-charset="UTF-8" action="http://localhost:3000/dashboard/courses/54633b9fc14ddd104c004de3/edit" enctype="multipart/form-data" method="post">

但是当我提交表单时出现此错误:

您要查找的页面不存在。

您可能输入了错误的地址或页面可能已移动。

我不明白为什么?谁能解释一下?

【问题讨论】:

    标签: ruby-on-rails forms routes


    【解决方案1】:

    另一种处理方法。在您的路线中写下:

    namespace :dashboard
      resources :courses, only: [:edit, :update]
    end
    

    你认为写:

    = form_tag [:dashboard, @course], multipart: true do |f| 
    

    然后你将使用 rails 默认值。

    【讨论】:

    • 更改路线可能需要重新启动您的 Rails 服务器。不然现在html里的表单怎么看?
    • 路线改进不错!
    【解决方案2】:

    您的表单声明使用post,但您没有配置post route

    rails 的方法是通过put 将表单提交到更新路径,因为你是updating a record

    = form_tag dashboard_update_course_path( @course), :method => 'put', :multipart => true do
    

    另外,你可能想use path instead of url

    然后只需命名update route

    namespace :dashboard do
      get   '/courses/:id/edit' => 'courses#edit', :as => :edit_course
      put   '/courses/:id/update' => 'courses#update', :as => :update_course
    end
    

    【讨论】:

    • 我尝试将表单更改为 method => 'put' 但它总是与 post 一起呈现。你知道为什么吗???
    • 取决于您使用的浏览器,may not be supported。你也可以用post 试试这个。这会起作用,但它不如put 正确。
    【解决方案3】:

    第一个参数是表单提交应该去哪里(更新)。

    http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多