How do you create links in Rails using link.to - ruby-on-rails

Hi im doing a kind of blog to learn Rails, using the Getting started tutorials like these
http://guides.rubyonrails.org/getting_started.html
http://guides.rubyonrails.org/routing.html
I have manage to do the posts sections and i also do a admin/posts section, this is the problem now..
Ths system is "conflicting" and admin goes to domain.com/posts instead of admin/posts.
I think the problem is the way i build the links..
In the tutorial to link a item yo do
<h2><%= link_to post.title, post %></h2>
I have tried
<h2><%= link_to post.title, admin_post_path %></h2>
And similars but i get
undefined local variable or method `admin_post_path' for #<#<Class:0x007fe3e990ef28>:0x007fe3e6e3b508>
How does this works i mean i have done rake routes and i see there the routes, but i cant use them
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_user GET /admin/users/new(.:format) admin/users#new
edit_user GET /admin/users/:id/edit(.:format) admin/users#edit
user GET /admin/users/:id(.:format) admin/users#show
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_post GET /admin/posts/new(.:format) admin/posts#new
edit_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
players GET /players(.:format) players#index
POST /players(.:format) players#create
new_player GET /players/new(.:format) players#new
edit_player GET /players/:id/edit(.:format) players#edit
player GET /players/:id(.:format) players#show
PUT /players/:id(.:format) players#update
DELETE /players/:id(.:format) players#destroy
player_steps GET /player_steps(.:format) player_steps#index
POST /player_steps(.:format) player_steps#create
new_player_step GET /player_steps/new(.:format) player_steps#new
edit_player_step GET /player_steps/:id/edit(.:format) player_steps#edit
player_step GET /player_steps/:id(.:format) player_steps#show
PUT /player_steps/:id(.:format) player_steps#update
DELETE /player_steps/:id(.:format) player_steps#destroy
coach_steps GET /coach_steps(.:format) coach_steps#index
POST /coach_steps(.:format) coach_steps#create
new_coach_step GET /coach_steps/new(.:format) coach_steps#new
edit_coach_step GET /coach_steps/:id/edit(.:format) coach_steps#edit
coach_step GET /coach_steps/:id(.:format) coach_steps#show
PUT /coach_steps/:id(.:format) coach_steps#update
DELETE /coach_steps/:id(.:format) coach_steps#destroy
candidates GET /candidates(.:format) candidates#index
POST /candidates(.:format) candidates#create
new_candidate GET /candidates/new(.:format) candidates#new
edit_candidate GET /candidates/:id/edit(.:format) candidates#edit
candidate GET /candidates/:id(.:format) candidates#show
PUT /candidates/:id(.:format) candidates#update
DELETE /candidates/:id(.:format) candidates#destroy
payment_notifications GET /payment_notifications(.:format) payment_notifications#show
post GET /posts/:id(.:format) posts#show
posts GET /posts(.:format) posts#index
admin_posts_path GET /admin/posts(.:format) admin/posts#index
admin_posts_path POST /admin/posts(.:format) admin/posts#index
admin_post_path GET /admin/posts/:id(.:format) admin/posts#show
new_admin_post_path GET /admin/posts/new(.:format) admin/posts#new
/*a(.:format) errors#routing
choose GET /user_type(.:format) home#user_type
root / devise/sessions#new
Also tried this
<h2><%= link_to post.title, url_for([#post]) %></h2>
this throw:::: Nil location provided. Can't build URI.
=( any documentation on doing this, ?? do you know where i can find it
Routes.rb
Consult::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
scope "/admin" do
resources :users, :controller => 'admin/users'
resources :posts, :controller => 'admin/posts'
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource :payment_notifications, :only => :show
#match 'candidates' => 'candidates#index'
#resources :posts
get '/posts/:id', to: 'posts#show', as: 'post'
get '/posts/', to: 'posts#index', as: 'posts'
get '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
post '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
get '/admin/posts/:id', to: 'admin/posts#show', as: 'admin_post_path'
get '/admin/posts/new', to: 'admin/posts#new', as: 'new_admin_post_path'
match '*a', :to => 'errors#routing'
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
#root :to => "devise/sessions#new"
get 'user_type', to: 'home#user_type', as: :choose
devise_scope :user do
root :to => "devise/sessions#new"
end
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
end

Try using namespace, as opposed to the scope, as the namespace is much better for nesting. your routes file should look something like this.
namespace :admin do
resources :users
resources :posts
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource :payment_notifications, :only => :show
#match 'candidates' => 'candidates#index'
resources :posts
you can remove
get '/posts/:id', to: 'posts#show', as: 'post'
get '/posts/', to: 'posts#index', as: 'posts'
get '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
post '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
get '/admin/posts/:id', to: 'admin/posts#show', as: 'admin_post_path'
get '/admin/posts/new', to: 'admin/posts#new', as: 'new_admin_post_path'
as the namespace and the resources handle their generation
full routes file should be
Consult::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
namespace :admin do
resources :users
resources :posts
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resources :posts
resource :payment_notifications, :only => :show
get 'user_type', to: 'home#user_type', as: :choose
devise_scope :user do
root :to => "devise/sessions#new"
end
end

Related

admin/login route is going post page

my routes.rb
get '/admin/login' => 'admin/sessions#new'
get '/admin/logout' => 'admin/sessions#destroy'
get '/admin' => 'admin/dashboard#index'
get 'blog' => 'blogs#index'
get ':id' => 'posts#show'
get 'posts/:id' => redirect('%{id}')
get 'blog/:id' => 'blogs#show'
get 'category/:id' => 'posts#index'
namespace :admin do
resources :sessions, only: [:new, :create, :destroy]
resources :login, only: [:index, :edit]
resources :categories
resources :admins, only: [:index, :edit, :update, :new, :create, :destroy]
resources :dashboard, only:[:index]
resources :settings, only:[:new, :create, :edit, :update]
resources :posts
resources :blogs
end
resources :home, only:[:index]
resources :posts, only:[:index, :show]
resources :blogs, only:[:index, :show]
when i try go to the /admin/login page it is seems like post page or something. but i can see the login form. just it is not admin page (i mean there is no my admin css or something it is seems like admin login form in my normal front end post page. and when i try login with my username and password it is giving this error:
ActionView::MissingTemplate in Admin::Posts#new
then im deleting this part from routes.rb
get 'blog' => 'blogs#index'
get ':id' => 'posts#show'
get 'posts/:id' => redirect('%{id}')
get 'blog/:id' => 'blogs#show'
get 'category/:id' => 'posts#index'
then i can go to the admin page. then im pasting this part again and still can use admin panel with no errors i dont understand.
can someone help? (sorry for my english)
here is the rake routes
Prefix Verb URI Pattern Controller#Action
root GET / home#index
admin_login GET /admin/login(.:format) admin/sessions#new
admin_logout GET /admin/logout(.:format) admin/sessions#destroy
admin GET /admin(.:format) admin/dashboard#index
blog GET /blog(.:format) blogs#index
GET /:id(.:format) posts#show
GET /posts/:id(.:format) redirect(301, %{id})
GET /blog/:id(.:format) blogs#show
GET /category/:id(.:format) posts#index
admin_sessions POST /admin/sessions(.:format) admin/sessions#create
new_admin_session GET /admin/sessions/new(.:format) admin/sessions#new
admin_session DELETE /admin/sessions/:id(.:format) admin/sessions#destroy
admin_login_index GET /admin/login(.:format) admin/login#index
edit_admin_login GET /admin/login/:id/edit(.:format) admin/login#edit
admin_categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_admin_category GET /admin/categories/new(.:format) admin/categories#new
edit_admin_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
admin_category GET /admin/categories/:id(.:format) admin/categories#show
PATCH /admin/categories/:id(.:format) admin/categories#update
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
admin_admins GET /admin/admins(.:format) admin/admins#index
POST /admin/admins(.:format) admin/admins#create
new_admin_admin GET /admin/admins/new(.:format) admin/admins#new
edit_admin_admin GET /admin/admins/:id/edit(.:format) admin/admins#edit
admin_admin PATCH /admin/admins/:id(.:format) admin/admins#update
PUT /admin/admins/:id(.:format) admin/admins#update
DELETE /admin/admins/:id(.:format) admin/admins#destroy
admin_dashboard_index GET /admin/dashboard(.:format) admin/dashboard#index
admin_settings POST /admin/settings(.:format) admin/settings#create
new_admin_setting GET /admin/settings/new(.:format) admin/settings#new
edit_admin_setting GET /admin/settings/:id/edit(.:format) admin/settings#edit
admin_setting PATCH /admin/settings/:id(.:format) admin/settings#update
PUT /admin/settings/:id(.:format) admin/settings#update
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PATCH /admin/posts/:id(.:format) admin/posts#update
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_blogs GET /admin/blogs(.:format) admin/blogs#index
POST /admin/blogs(.:format) admin/blogs#create
new_admin_blog GET /admin/blogs/new(.:format) admin/blogs#new
edit_admin_blog GET /admin/blogs/:id/edit(.:format) admin/blogs#edit
admin_blog GET /admin/blogs/:id(.:format) admin/blogs#show
PATCH /admin/blogs/:id(.:format) admin/blogs#update
PUT /admin/blogs/:id(.:format) admin/blogs#update
DELETE /admin/blogs/:id(.:format) admin/blogs#destroy
home_index GET /home(.:format) home#index
posts GET /posts(.:format) posts#index
post GET /posts/:id(.:format) posts#show
blogs GET /blogs(.:format) blogs#index
GET /blogs/:id(.:format) blogs#show
Seems like the issue is on these two lines.
get ':id' => 'posts#show'
get 'posts/:id' => redirect('%{id}')
Try this
get 'posts/:id' => 'posts#show'

routing with non-restful actions issue rails

I have encountered a routing issue with rails. I'll explain as best as I can.
a user can use the create and index actions of the entries controller.
authenticated with devise, an admin can perform the rest of the restful actions on the entries controller. Furthermore, there are an additional three pages which I wish to add, 'pending, approved, rejected', so I added these to my entries controller like so:
class EntriesController < ApplicationController
before_action :get_entries, only: [:index, :pending, :approved, :rejected]
expose(:entry){#entry}
expose(:entries){#entries}
def create
#entry = Photo.new(params_entry)
if #entry.save
record_saved
return redirect_to(entries_path)
else
check_for_errors
return render('new')
end
end
def index
end
def new
#entry = Photo.new
end
def show
end
def pending
end
def approved
end
def rejected
end
private
def params_photo
params.require(:photo).permit!
end
def get_photos
#entries = Entry.all
end
end
(note, this controller is incomplete, but for the purposes of my question it suffices)
I am trying to configure my routes so that the path to view these pages are like
http://localhost:3000/admin/entries/pending
so I have configured my routes like so:
devise_for :admins # The priority is based upon order of creation: first created -> highest priority.
root 'home#index'
resources :entries, :only => [:index, :new]
namespace :admin do
resources :entries do
collection do
match :pending, :via => [:get, :post]
match :rejected, :via => [:get, :post]
match :approved, :via => [:get, :post]
end
end
end
except I am getting the error
uninitialized constant Admin::EntriesController
expect if i change 'admin' to plural 'admins' and configure my view path accordingly I get this error:
No route matches [GET] "/admin/entries/pending"
why is this happening? Am i making the correct use of 'namespace' and 'collection'. How do I fix this issue?
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
new_entry GET /entries/new(.:format) entries#new
pending_admin_entries GET|POST /admin/entries/pending(.:format) admin/entries#pending
rejected_admin_entries GET|POST /admin/entries/rejected(.:format) admin/entries#rejected
approved_admin_entries GET|POST /admin/entries/approved(.:format) admin/entries#approved
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
FOLDER STRUCTURE
controllers > admin_controller.rb
admin > entries_controller.rb (this is the one with pending etc)
entries_controller.rb (this is the public one with index etc)
views > admins > index.html.erb
entries > pending.html.erb, rejected.html.erb etc
Thanks

Two Devise Models, With Association Issue

I have two models for devise. Client, and Engineer.
The Client belongs to the Engineer. From the Engineer#show page, I'm trying to link to the client but I'm getting a devise mapping error.
Could not find devise mapping for path "/clients/10".
I have two controllers, one for devise and one regular (controllers/client/registration_controller and controllers/client_controller)
My routes file looks like this...
devise_for :clients, :controllers => {:registrations => "client/registrations"}
resources :clients, :only => [:show, :new, :create]
devise_for :engineers, :controllers => {:registrations => "engineer/registrations"}
resources :engineers, :only => [:show]
devise_scope :client do
get "clients/:id", to: "clients#show"
end
Are the resources :clients competing with the scope declaration?
For good measure my rake routes:
new_client_session GET /clients/sign_in(.:format) devise/sessions#new
client_session POST /clients/sign_in(.:format) devise/sessions#create
destroy_client_session DELETE /clients/sign_out(.:format) devise/sessions#destroy
client_password POST /clients/password(.:format) devise/passwords#create
new_client_password GET /clients/password/new(.:format) devise/passwords#new
edit_client_password GET /clients/password/edit(.:format) devise/passwords#edit
PATCH /clients/password(.:format) devise/passwords#update
PUT /clients/password(.:format) devise/passwords#update
cancel_client_registration GET /clients/cancel(.:format) client/registrations#cancel
client_registration POST /clients(.:format) client/registrations#create
new_client_registration GET /clients/sign_up(.:format) client/registrations#new
edit_client_registration GET /clients/edit(.:format) client/registrations#edit
PATCH /clients(.:format) client/registrations#update
PUT /clients(.:format) client/registrations#update
DELETE /clients(.:format) client/registrations#destroy
clients POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
client GET /clients/:id(.:format) clients#show
new_engineer_session GET /engineers/sign_in(.:format) devise/sessions#new
engineer_session POST /engineers/sign_in(.:format) devise/sessions#create
destroy_engineer_session DELETE /engineers/sign_out(.:format) devise/sessions#destroy
engineer_password POST /engineers/password(.:format) devise/passwords#create
new_engineer_password GET /engineers/password/new(.:format) devise/passwords#new
edit_engineer_password GET /engineers/password/edit(.:format) devise/passwords#edit
PATCH /engineers/password(.:format) devise/passwords#update
PUT /engineers/password(.:format) devise/passwords#update
engineer GET /engineers/:id(.:format) engineers#show
GET /clients/:id(.:format) clients#show
root GET / pages#home
pages_clients GET /pages/clients(.:format) pages#clients
pages_engineers GET /pages/engineers(.:format) pages#engineers
I'm pretty sure you should just drop your devise_scope block, since you have the following line:
resources :clients, :only => [:show, :new, :create]
This line already gives you client_path:
client GET /clients/:id(.:format) clients#show
but your devise_scope gives you a duplicate (but unnamed) path:
GET /clients/:id(.:format) clients#show
So, your routes file should look like:
devise_for :clients, :controllers => {:registrations => "client/registrations"}
resources :clients, :only => [:show, :new, :create]
devise_for :engineers, :controllers => {:registrations => "engineer/registrations"}
resources :engineers, :only => [:show]

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

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