Better way to organize rails routes? - ruby-on-rails

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

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.

adding a method to non-restful routes in rails 4

i would like to perform an action on some of my routes, the problem is they are not in a 'resources' block because I need named methods for each. I want to be able to toggle the state of each an attribute in each item in an index type view. I wwas attempting to incorporate this tutorial.
devise_for :admins # The priority is based upon order of creation: first created -> highest priority.
root 'home#index'
resources :entries, :only => [:index, :new, :create]
namespace :admin do
namespace :entries do
match :pending, :via => [:get, :post], :collection => { :toggle_approve => :put}
match :rejected, :via => [:get, :post], :collection => { :toggle_approve => :put}
match :approved, :via => [:get, :post], :collection => { :toggle_approve => :put}
end
end
entries controller
class Admin::EntriesController < ApplicationController
expose(:entries){#entries}
def index
end
def show
end
def approved
#entries = Photo.with_approved_state
end
def pending
#entries = Photo.with_pending_state
end
def rejected
#entries = Photo.with_rejected_state
end
def toggle_approve
#a = Photo.find(params[:id])
#a.toggle!(:workflow_state)
render :nothing => true
end
rake routes
Prefix Verb URI Pattern Controller#Action
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PATCH /admins/password(.:format) devise/passwords#update
PUT /admins/password(.:format) devise/passwords#update
cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel
admin_registration POST /admins(.:format) devise/registrations#create
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit
PATCH /admins(.:format) devise/registrations#update
PUT /admins(.:format) devise/registrations#update
DELETE /admins(.:format) devise/registrations#destroy
root GET / home#index
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
admin_entries_pending GET|POST /admin/entries/pending(.:format) admin/entries#pending {:collection=>{:toggle_approve_article=>:put}}
admin_entries_rejected GET|POST /admin/entries/rejected(.:format) admin/entries#rejected {:collection=>{:toggle_approve_article=>:put}}
admin_entries_approved GET|POST /admin/entries/approved(.:format) admin/entries#approved {:collection=>{:toggle_approve_article=>:put}}
I don't know where the collection option is from (I literally can't find reference to it anywhere)
Implementing non-resourceful routes is actually relatively simple:
#config/routes.rb
resources :entries, only: [:index, :new, :create] do
collection do
match :pending, via: [:get, :post] #-> domain.com/entries/pending
match :rejected, via: [:get, :post] #-> domain.com/entries/rejected
match :approved, via: [:get, :post] #-> domain.com/entries/approved
end
end
I don't understand the collection option - I don't think that belongs in your routes. Although having thought about it, I guess you're trying to make it so that if you receive a request to domain.com/entries/toggle_approve_article/pending you'll want to handle the reqeust?
If that's the case, why not just do this:
#config/routes.rb
resources :entries, only: [:index, :new, :create] do
put :toggle_approve #-> domain.com/entries/15/toggle_approve
collection do
match :pending, via: [:get, :post] #-> domain.com/entries/pending
match :rejected, via: [:get, :post] #-> domain.com/entries/rejected
match :approved, via: [:get, :post] #-> domain.com/entries/approved
end
end
i had to understand the difference between member and collections. collections are routes additional to a resource (eg, the restful actions). members are extra routes which can perform actions on their the block.
This...
resources :entries, :only => [:index, :new, :create]
namespace :admin do
resources :entries do
get :pending, on: :collection
get :approved, on: :collection
get :rejected, on: :collection
member do
get :toggle_approve_field
get :toggle_reject_field
end
end
end
yielded this rake routes
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
pending_admin_entries GET /admin/entries/pending(.:format) admin/entries#pending
approved_admin_entries GET /admin/entries/approved(.:format) admin/entries#approved
rejected_admin_entries GET /admin/entries/rejected(.:format) admin/entries#rejected
toggle_approve_field_admin_entry GET /admin/entries/:id/toggle_approve_field(.:format) admin/entries#toggle_approve_field
toggle_reject_field_admin_entry GET /admin/entries/:id/toggle_reject_field(.:format) admin/entries#toggle_reject_field
admin_entries GET /admin/entries(.:format) admin/entries#index
POST /admin/entries(.:format) admin/entries#create
new_admin_entry GET /admin/entries/new(.:format) admin/entries#new
edit_admin_entry GET /admin/entries/:id/edit(.:format) admin/entries#edit
admin_entry GET /admin/entries/:id(.:format) admin/entries#show
PATCH /admin/entries/:id(.:format) admin/entries#update
PUT /admin/entries/:id(.:format) admin/entries#update
DELETE /admin/entries/:id(.:format) admin/entries#destroy
It took a lot of faffing around and I not sure I'd be able to do it upon request without more headache but I certainly have a better idea of rails routing all together. Thanks for the help from #RichPeck

Username in urls for nested routes

I'm a new rails developer making an application where I have a user profile like: localhost:3000/posts routing to localhost:3000/Charles where Charles is the username.
So the code I'm using to do this looks like:
routes.rb
match "/:username" => "posts#index"
and then in my controller:
#user = User.find_by_username params[:username]
#search = Post.search do |query|
query.fulltext params[:search]
query.with(:user, #user.id)
end
this works great just for the post index, but I'd like to have posts/18 (for example) be routed to /username/posts/18.
So basically, I'm asking if there's a way to do something like this:
match "/:username" => "posts#index" do
resources :posts
end
Thanks for all help!
This
scope '/:username' do
resources :posts
end
produces what you want:
posts GET /:username/posts(.:format) posts#index
POST /:username/posts(.:format) posts#create
new_post GET /:username/posts/new(.:format) posts#new
edit_post GET /:username/posts/:id/edit(.:format) posts#edit
post GET /:username/posts/:id(.:format) posts#show
PATCH /:username/posts/:id(.:format) posts#update
PUT /:username/posts/:id(.:format) posts#update
DELETE /:username/posts/:id(.:format) posts#destroy
Try
scope ':username' do
resources :posts
end
bundle exec rake routes
posts GET /:username/posts(.:format) posts#index
POST /:username/posts(.:format) posts#create
new_post GET /:username/posts/new(.:format) posts#new
edit_post GET /:username/posts/:id/edit(.:format) posts#edit
post GET /:username/posts/:id(.:format) posts#show
PUT /:username/posts/:id(.:format) posts#update
DELETE /:username/posts/:id(.:format) posts#destroy
match "/:username/posts/:id" => "posts#show"
But that won't really help when you need to edits and such. So, try:
scope "/:username" do
resources :posts
end
That failing:
scope "/:username" do
get '' => "posts#index"
get 'posts/:id' => "posts#show"
end
Hopefully the first will work. Haven't tried it myself so forgive me, but I'm pretty sure that, in general, scope is what you want.

Changing every route from /product to /eshop in Rails Route 3

in my routes.rb i have foll entries:-
resources :products do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
i want to change every /products to /eshop in the url.
i am not sure but can i use :path=>:eshop.Will it will be also applicable to the sub routes as well such as eshop/get_products,eshop/buy_this...etc.
You can modify your routes and run rake routes in the terminal to check the paths.
resources :products, :path => 'eshop', :as => 'eshop' do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
will produce these
get_products_eshop_index GET /eshop/get_products(.:format) products#get_products
show_product_details_eshop GET /eshop/:id/show_product_details(.:format) products#show_product_details
buy_this_eshop_index GET /eshop/buy_this(.:format) products#buy_this
search_product_eshop_index GET /eshop/search_product(.:format) products#search_product
eshop_index GET /eshop(.:format) products#index
POST /eshop(.:format) products#create
new_eshop GET /eshop/new(.:format) products#new
edit_eshop GET /eshop/:id/edit(.:format) products#edit
eshop GET /eshop/:id(.:format) products#show
PUT /eshop/:id(.:format) products#update
DELETE /eshop/:id(.:format) products#destroy

How to remove controller names from rails routes?

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.

Resources