How to remove controller names from rails routes? - ruby-on-rails

I would like to trim down the routes on my application so that:
http://myapplication.com/users/peter/questions/how-do-i-create-urls
becomes...
http://myapplication.com/peter/how-do-i-create-urls
I have a users controller and would like it to be resourceful. Users also have a nested resource called questions.
Basic routes file
Without any URL trimming, the routes file looks like this:
...
resources :users do
resources :questions
end
However the URLs from this take the form of
http://myapplication.com/users/peter/questions/how-do-i-create-urls
rather than
http://myapplication.com/peter/how-do-i-create-urls
Partial success
I have tried doing the following:
...
resources :users, :path => '' do
resources :questions
end
This works and produces:
http://myapplication.com/peter/questions/how-do-i-create-urls
However if I try:
...
resources :users, :path => '' do
resources :questions, :path => ''
end
Then things start to go wrong.
Is this the right approach and if so, can it be made to work with nested resources too?

The way you are doing it should work. I don't know what problem you are experiencing but if you copied the example code from your app directly then it might be because of the extra end that you have put in your routes. It should probably look like this:
resource :users, :path => '' do
resource :questions, :path => ''
end
Another thing that could be the cause and that you need to be vary careful about is that these routes pretty much catches all requests and you should have them last in your routes.rb so that other routes matches first. Take this scenario for example:
resource :users, :path => '' do
resource :questions, :path => ''
end
resources :posts
If you do it this way then no request will ever be routed to the Posts controller since a request to /posts/1 will be sent to the Questions controller with :user_id => 'posts', :id => 1
Edit:
Also, I now noticed that you use resource instead of resources. Don't know if that is intended or if it is a mistake.

Thanks to both #mark and #DanneManne for their help. With their input and a little more tweaking I got it all working. It's not exactly trivial but I'm not sure you could make it much shorter either:
Final working code
# setup the basic resources while holding some back for creation below
resources :users, :except => [:show, :index, :new, :create], :path => '/' do
resources :questions, :except => [:show]
end
# for clarity, pick out routes that would otherwise go
# to root (such as new_user => '/new')
resources :users, :only => [:index, :new, :create]
# setup questions#show to give clean URLS
match ':user_id/:question_id', :as => :user_question,
:via => :get,
:controller => :questions,
:action => :show
# setup users#show method to give clean URLS
match ':user_id', :as => :user,
:via => :get,
:controller => :user,
:action => :show
Rake Routes output
user_questions GET /:user_id/questions(.:format) {:action=>"index", :controller=>"questions"}
POST /:user_id/questions(.:format) {:action=>"create", :controller=>"questions"}
new_user_question GET /:user_id/questions/new(.:format) {:action=>"new", :controller=>"questions"}
edit_user_question GET /:user_id/questions/:id/edit(.:format) {:action=>"edit", :controller=>"questions"}
user_question PUT /:user_id/questions/:id(.:format) {:action=>"update", :controller=>"questions"}
DELETE /:user_id/questions/:id(.:format) {:action=>"destroy", :controller=>"questions"}
edit_user GET /:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user PUT /:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /:id(.:format) {:action=>"destroy", :controller=>"users"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
user_question GET /:user_id/:question_id(.:format) {:controller=>"questions", :action=>"show"}
user GET /:user_id(.:format) {:controller=>"user", :action=>"show"}

Not sure about the nesting but try
:path => '/'

Just thought I'd add another possible solution, in case anybody else arrives from Google at a later date.
After looking at this nested resource article, this could be a cleaner solution with almost the same flexibility in routes:
resources :users, :path => ''
resources :users, :path => '', :only => [] do
resources :questions, :path => '', :except => [:index]
Basically, by including the second parent block, the child resources aren't yielded before the parent resources.
This particular example also assumes that having complete routes for the parent block is more crucial than those for the child. Consequently, the child block is limited from having an index, but this might be acceptable, depending on the situation.

Related

Rails - missing routes helper prefix

I am missing the contacts#show prefix 'contact' as seen below.
rake routes
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
I am thinking this is the reason why I am getting a dot instead of a slash when clicking the following link.
_contact.html.erb
<%= link_to "delete contact", contact, method: :delete,
data: { confirm: "You sure?" } %>
The server logs the proper DELETE request, however, cannot render /contact.26 instead of the correct /contacts/26.
Started DELETE "/contact.26" for 128.177.12.30 at 2016-04-13 21:04:30 +0000
ActionController::RoutingError (No route matches [DELETE] "/contact.26"):
Every post I've come across with a dot instead of a slash seems to stem from a pluralization error, however, I don't believe this is the case here.
In addition, I've removed resources :contacts from my routes file, run $ rake routes, added resources :contacts, run $ rake routes, and the problem persists.
This problem seems to be unique to the contacts model, as the rest of my models aren't missing any prefixes or having this error when deleting.
How do I add the 'contact' prefix back to 'contacts#show'?
routes.rb file for reference:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'newevent' => 'events#new'
get 'newteam' => 'teams#new'
get 'newperformance' => 'performances#new'
get 'newhotel' => 'hotels#new'
get 'newcontact' => 'contacts#new'
get 'newflight' => 'flights#new'
get 'newground' => 'grounds#new'
get 'newguest' => 'guests#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :events
resources :teams do
member do
get :events
end
end
resources :performances
resources :hotels
resources :contacts
resources :flights
resources :grounds
resources :guests
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
you may have a conflict between the routes set by
resources :contacts
and
get 'contact' => 'static_pages#contact'
The easiest solution is probably to change the static page's links. Best to keep the resources together if you can.
I just came across this same error.
the fix to my issue was that 's' at the end of resources
I had 'resource', and getting the same problem as you.
I changed it to 'resources' and it fixed my problem
in my case was just a typo.
This error showed up for me yesterday. What fixed it was deleting helper files that were not being used. Might be worth a try for you.

How to to route "nested" paths by using `namespace` or `scope :module` / `scope :path`?

I am using Ruby on Rails 4.2 and I would like to route "nested" paths by using namespace or scope :module / scope :path within the block of a resource.
That is, I have the following route:
resources :users, :only => [:show]
that matches
user_path GET /users/:id(.:format) users#show
I would like to match the following paths
users_sessions_path POST /users/sessions users/sessions#create
user_session_path GET /users/:id/session users/sessions#show
delete_user_session_path GET /users/:id/session/delete users/sessions#delete
user_session_path DELETE /users/:id/session users/sessions#destroy
I read the official documentation and I tried to state something like
resources :users, :only => [:show] do
scope :module => :users do
scope :module => :sessions do
# scope :path => :sessions do
# namespace :sessions do
...
end
end
end
but no attempt has been successful. How should I state routes?
Update after the #dgilperez answer
I tried the following code
resources :users, :only => [:show] do
scope :module => :users do
resource :session, :only => [:show, :new, :create, :destroy] do
get :delete, :on => :collection, :to => 'sessions#delete'
end
end
end
that matches
delete_user_session_path GET /users/:user_id/session/delete(.:format) users/sessions#delete
new_user_session_path GET /users/:user_id/session/new(.:format) users/sessions#new
user_session_path POST /users/:user_id/session(.:format) users/sessions#create
user_session_path GET /users/:user_id/session(.:format) users/sessions#show
DELETE /users/:user_id/session(.:format) users/sessions#destroy
but I still need to map new and create actions without needing to pass the :user_id parameter. That is, I would like to map something like
new_user_session_path GET /users/session/new(.:format) users/sessions#new
user_session_path POST /users/session(.:format) users/sessions#create
I think you are overcomplicating it: you don't need to use scope or path to render nested resources. You just nest them:
resources :users, :only => [:show] do
resources :sessions
end
will render the following routes:
user_sessions GET /users/:user_id/sessions(.:format) sessions#index
POST /users/:user_id/sessions(.:format) sessions#create
new_user_session GET /users/:user_id/sessions/new(.:format) sessions#new
edit_user_session GET /users/:user_id/sessions/:id/edit(.:format) sessions#edit
user_session GET /users/:user_id/sessions/:id(.:format) sessions#show
PATCH /users/:user_id/sessions/:id(.:format) sessions#update
PUT /users/:user_id/sessions/:id(.:format) sessions#update
DELETE /users/:user_id/sessions/:id(.:format) sessions#destroy
user GET /users/:id(.:format) users#show
Those are not the routes you mention you need, but I'd like you to reconsider if you really need them like that, with namespaced controllers and custom names such as delete_user_ or you prefer to go more standard. If you really need those exact routes, let me know.
UPDATE after OP's update
To get the two routes missing you need to get them out the rested resource. I'd just write them directly like this:
resources :users, :only => [:show] do
scope :module => :users do
resource :session, :only => [:show, :destroy] do
get :delete, :on => :collection, :to => 'sessions#delete'
end
end
end
get 'users/session/new', to: 'users/sessions#new', as: :new_user_session
post 'users/session', to: 'users/sessions#create'

Better way to organize rails routes?

I was just wondering if there was a better way to organize these rails routes. Perhaps in a group? I'm unfamiliar with the best practices of something like this.
TestApp::Application.routes.draw do
root :to => 'posts#index'
resources :posts, :except => :destroy
match '/posts/page/:page' => 'posts#page'
match '/posts/delete/:id' => 'posts#delete'
match '/posts/undelete/:id' => 'posts#undelete'
match '/posts/hide/:id' => 'posts#hide'
match '/posts/unhide/:id' => 'posts#unhide'
match '/help' => 'help#index'
end
Yes, you can group them in a collection block nested within a block on resources :posts as follows
resources :posts, :except => :destroy do
collection do
match 'page/:page' => 'posts#page'
match 'delete/:id' => 'posts#delete'
match 'undelete/:id' => 'posts#undelete'
match 'hide/:id' => 'posts#hide'
match 'unhide/:id' => 'posts#unhide'
end
end
These routes aren't very RESTful though. I might suggest something like the following
resources :posts, :except => :destroy do
member do
get 'page/:page' => 'posts#page'
delete 'delete' => 'posts#delete'
put 'undelete' => 'posts#undelete'
put 'hide' => 'posts#hide'
put 'unhide' => 'posts#unhide'
end
end
This creates cleaner routes while still pointing to the same controller/action. You will have to modify your application to point to the correct routes a bit differently to support this more accepted behavior though.
GET /posts/:id/page/:page(.:format) posts#page
delete_post DELETE /posts/:id/delete(.:format) posts#delete
undelete_post PUT /posts/:id/undelete(.:format) posts#undelete
hide_post PUT /posts/:id/hide(.:format) posts#hide
unhide_post PUT /posts/:id/unhide(.:format) posts#unhide
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update

Rails routing url name helpers

What basic settings are required to make sure routing url name helpers work?
For instance in my route I have the following:
Blog::Application.routes.draw do
resources :news, :as => :news_items, :controller => :news_items, :only => [:show, :index]
scope :module => "refinery" do
scope(:path => 'refinery', :as => 'admin', :module => 'Admin') do
resources :news, :except => :show, :as => :news_items, :controller => :news_items
end
end
end
but the following doesn't seem to work:
new_refinery_news_url
I keep on getting the error
undefined local variable or method `new_refinery_news_url'
So I'm pretty sure something is missing in the way I have configured my application, who's main routing is in the RefineryCMS gem which was added in the Gemfile.
Any thoughts?
Had to use main_app.new_refinery_news_url instead.
The helper name will be new_admin_news_item_url.
It's simple to find all routes and their helper methods. Just run rake routes and you will see:
news_items GET /news(.:format) {:action=>"index", :controller=>"news_items"}
news_item GET /news/:id(.:format) {:action=>"show", :controller=>"news_items"}
admin_news_items GET /refinery/news(.:format) {:action=>"index", :controller=>"refinery/Admin/news_items"}
POST /refinery/news(.:format) {:action=>"create", :controller=>"refinery/Admin/news_items"}
new_admin_news_item GET /refinery/news/new(.:format) {:action=>"new", :controller=>"refinery/Admin/news_items"}
edit_admin_news_item GET /refinery/news/:id/edit(.:format) {:action=>"edit", :controller=>"refinery/Admin/news_items"}
admin_news_item PUT /refinery/news/:id(.:format) {:action=>"update", :controller=>"refinery/Admin/news_items"}
DELETE /refinery/news/:id(.:format) {:action=>"destroy", :controller=>"refinery/Admin/news_items"}
With mountable engines you always need to specify "main_app." (or for Refinery routes "refinery.") prefix because engines are isolated from the application.
A solution, if you're using routes outside of refinery, is to prefix the named_path with the Rails object that contains the methods for named routes
Rails.application.routes.url_helpers.new_admin_news_item_path

Can't access path specified by rake routes command

I can't seem to correctly understand the routing in rails 3.1.
(keeping in mind I'm working on a project that depends on the refinery cms gem)
In my routes I have the following:
Blog::Application.routes.draw do
resources :news, :as => :news_items, :controller => :news_items, :only => [:show, :index]
scope :module => "refinery" do
scope(:path => 'refinery', :as => 'refinery_admin', :module => 'admin') do
resources :news, :as => :news_items, :controller => :news_items
#resources :news, :except => :show, :as => :news_items, :controller => :news_items
end
end
end
The output of the rake routes command is:
news_items GET /news(.:format) {:action=>"index", :controller=>"news_items"}
news_item GET /news/:id(.:format) {:action=>"show", :controller=>"news_items"}
refinery_admin_news_items GET /refinery/news(.:format) {:action=>"index", :controller=>"refinery/admin/news_items"}
POST /refinery/news(.:format) {:action=>"create", :controller=>"refinery/admin/news_items"}
new_refinery_admin_news_item GET /refinery/news/new(.:format) {:action=>"new", :controller=>"refinery/admin/news_items"}
edit_refinery_admin_news_item GET /refinery/news/:id/edit(.:format) {:action=>"edit", :controller=>"refinery/admin/news_items"}
refinery_admin_news_item GET /refinery/news/:id(.:format) {:action=>"show", :controller=>"refinery/admin/news_items"}
PUT /refinery/news/:id(.:format) {:action=>"update", :controller=>"refinery/admin/news_items"}
DELETE /refinery/news/:id(.:format) {:action=>"destroy", :controller=>"refinery/admin/news_items"}
The following code in my application leads to an error:
<%= form_for [:refinery, #news_item] do |f| %>
<% end %>
Telling me that the following path:
undefined method `refinery_news_items_path' for #<#<Class:0x0000010663c480>:0x00000106623980>
doesn't exist.
Any path from the rake routes command would just not work. I'm pretty confident that's it's a setting issue. I'm actually kind of writing a plugin to the RefineryCMS gem without actually using the plugin generator, I'm just building it as if it was a normal web app.
My project is hosted here for those who care to take a peak. http://github.com/mabounassif/blog
Anyone knows what might be the problem?
You're scoping your "refinery" scope as "refinery_admin" so when you did your rake routes, you got refinery_admin_news_items
if you take out that :as => 'refinery_admin' clause, your routes will return back to "normal"
It seems that the problem was with the way the Refinery CMS gem works. Apparently I shouldn't use the url helper right away, I should use the following instead:
main_app.new_refinery_admin_news_item_path

Resources