0

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?

davegson
  • 8,205
  • 4
  • 51
  • 71
Jean
  • 5,201
  • 11
  • 51
  • 87

3 Answers3

2

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.

nathanvda
  • 49,707
  • 13
  • 117
  • 139
1

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
Community
  • 1
  • 1
davegson
  • 8,205
  • 4
  • 51
  • 71
  • I try to change the form to method => 'put' but it always render with post. Do you know why??? – Jean Nov 17 '14 at 11:21
  • Depending on what browser you use, it [may not be supported](http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers). You could also try this with `post`. This will work, but it's not as correct as `put`. – davegson Nov 17 '14 at 11:23
0

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

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

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70