简体   繁体   中英

“The page you were looking for doesn't exist.” edit form

I have the following route

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

and this form

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

the action being:

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

But when I submit the form I get this error:

The page you were looking for doesn't exist.

You may have mistyped the address or the page may have moved.

I don't understand why? Could somebody explain?

An alternative way to handle this. In your routes write:

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

And in your view write:

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

Then you will use rails defaults.

Your form states to use post , but you don't have a post route configured.

The rails way to do this is to submit the form to the update path via put , since you are updating a record :

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

Also, you probably want to use path instead of url .

Then just name the update route :

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

The first parameter is where the form submission should go (update).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM