Rails Routing Question - "No route matches..."

682 views Asked by At

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.

1

There are 1 answers

1
MDaubs On BEST ANSWER

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)