0

I've got a model "Job" which i'm adding a new action "preview" to. In order to support this new action, i've modifed routes.rb as follows:

resources :jobs do
   member do
      get 'preview'
   end
end

Then on my Job create action i've got the following:

 if @job.save
    redirect_to preview_job_url

However, when I save the form, instead of redirecting to the preview url, I get the following routing error:

Routing Error
No route matches {:action=>"preview", :controller=>"jobs"}

Can't figure out why this is happening, as the URL works properly (http://localhost:3000/jobs/id/preview) and if I run rake routes the proper route seems to be there:

preview_jobs GET    /jobs/preview(.:format)            {:action=>"preview", :controller=>"jobs"}

Any ideas as to what could be happening? It seems like all the pieces are in place, but I'm new to Rails so i'm sure i'm missing something obvious. Many thanks.

cman77
  • 1,753
  • 1
  • 22
  • 48

1 Answers1

3

You defined 'preview' as a member action. Member actions refer to an instance of a model. When you call preview_job_url you need to pass along a specific Job object or the ID of a Job so that the URL can be created. This should make sense... how can Rails build you a URL that references a specific model if you don't tell the framework which Job you want to build a URL for?

Try this in your controller:

if @job.save
  redirect_to preview_job_url(@job)
MDaubs
  • 2,984
  • 1
  • 17
  • 12
  • In your original question, your snippet from rake routes does not line up with your snippet from routes.rb. `preview_jobs => /jobs/preview(.:format)` is an action on the collection of jobs while `preview_job => /jobs/:id/preview(.:format)` is an action on a member of the jobs collection. In other words, `preview_job_url(@job) => "/jobs/1234/preview"` and `preview_jobs_url => "/jobs"` assuming that both are defined in routes.rb (which would be strange). Make sense? It's a little tricky to get used. We've all been there. I suspect you have an extra "s" someplace. – MDaubs Mar 07 '11 at 00:15