Note that there are some explanatory texts on larger screens.

plurals
  1. POShallow routes with path_prefix?
    text
    copied!<p>Recently I changed a few nested resources in one of my applications to use shallow routing. It's working great and I've been able to simplify my views and controllers.</p> <p>However, I've been using a path_prefix before:</p> <pre><code>map.with_options :path_prefix =&gt; "blog" do |blog| blog.resources :posts do |posts| posts.resources :comments end end </code></pre> <p>Notice, that all routes are prefixed with "/blog" as expected.</p> <pre><code># $ rake routes # posts GET /blog/posts(.:format) {:controller=&gt;"posts", :action=&gt;"index"} # POST /blog/posts(.:format) {:controller=&gt;"posts", :action=&gt;"create"} # new_post GET /blog/posts/new(.:format) {:controller=&gt;"posts", :action=&gt;"new"} # edit_post GET /blog/posts/:id/edit(.:format) {:controller=&gt;"posts", :action=&gt;"edit"} # post GET /blog/posts/:id(.:format) {:controller=&gt;"posts", :action=&gt;"show"} # PUT /blog/posts/:id(.:format) {:controller=&gt;"posts", :action=&gt;"update"} # DELETE /blog/posts/:id(.:format) {:controller=&gt;"posts", :action=&gt;"destroy"} # post_comments GET /blog/posts/:post_id/comments(.:format) {:controller=&gt;"comments", :action=&gt;"index"} # POST /blog/posts/:post_id/comments(.:format) {:controller=&gt;"comments", :action=&gt;"create"} # new_post_comment GET /blog/posts/:post_id/comments/new(.:format) {:controller=&gt;"comments", :action=&gt;"new"} # edit_post_comment GET /blog/posts/:post_id/comments/:id/edit(.:format) {:controller=&gt;"comments", :action=&gt;"edit"} # post_comment GET /blog/posts/:post_id/comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"show"} # PUT /blog/posts/:post_id/comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"update"} # DELETE /blog/posts/:post_id/comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"destroy"} </code></pre> <p>The new routing configuration looks like this:</p> <pre><code>map.with_options :path_prefix =&gt; "blog", :shallow =&gt; true do |blog| blog.resources :posts do |posts| posts.resources :comments end end </code></pre> <p>Now, the "/blog" prefix is missing in some of my routes.</p> <pre><code># $ rake routes # posts GET /blog/posts(.:format) {:controller=&gt;"posts", :action=&gt;"index"} # POST /blog/posts(.:format) {:controller=&gt;"posts", :action=&gt;"create"} # new_post GET /blog/posts/new(.:format) {:controller=&gt;"posts", :action=&gt;"new"} # edit_post GET /posts/:id/edit(.:format) {:controller=&gt;"posts", :action=&gt;"edit"} # post GET /posts/:id(.:format) {:controller=&gt;"posts", :action=&gt;"show"} # PUT /posts/:id(.:format) {:controller=&gt;"posts", :action=&gt;"update"} # DELETE /posts/:id(.:format) {:controller=&gt;"posts", :action=&gt;"destroy"} # post_comments GET /posts/:post_id/comments(.:format) {:controller=&gt;"comments", :action=&gt;"index"} # POST /posts/:post_id/comments(.:format) {:controller=&gt;"comments", :action=&gt;"create"} # new_post_comment GET /posts/:post_id/comments/new(.:format) {:controller=&gt;"comments", :action=&gt;"new"} # edit_comment GET /comments/:id/edit(.:format) {:controller=&gt;"comments", :action=&gt;"edit"} # comment GET /comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"show"} # PUT /comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"update"} # DELETE /comments/:id(.:format) {:controller=&gt;"comments", :action=&gt;"destroy"} </code></pre> <p>I'm looking for a solution to get the prefixes back for all routes. I know that it's working with namespaces (<code>map.namespace :blog do</code>), but I want to prevent refactoring all my controllers/views/tests to actually use namespaces.</p> <p>All code samples are tested with Rails version 2.3.2 and Ruby 1.8.7.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload