In my rails app following in routes.rb
resources :users
leads to following output for 'rake routes'
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
& following in routes.rb (for my custom controller 'home')
match '/new_user' => 'home#new_user', via: [:get]
match '/users/:id/edit' => 'home#edit_user', via: [:get]
match '/users/:id' => 'home#show_user', via: [:get]
match '/users/:id' => 'home#create_user', via: [:post]
leads to following output for 'rake routes'
GET /new_user(.:format) home#new_user
GET /users/:id/edit(.:format) home#edit_user
GET /users/:id(.:format) home#show_user
POST /users/:id(.:format) home#create_user
why there are no path names for second case? like in first case ('new_user', 'edit_user')
is there any way to have path names for second case? as i want to use these path names in my views
There are no path names because you haven't specified path names. If you're supplying custom routes instead of using resources, you need to use :as to provide a pathname:
match '/new_user' => 'home#new_user', via: :get, as: :new_user
You should also just use get instead of match... via: :get:
get '/new_user' => 'home#new_user', as: :new_user
However, given your set of routes, your better bet is to continue using resources, but to supply a limited list of actions via :only and a custom controller via :controller:
resources :users, only: %w(new edit show create), controller: "home"
Related
Rails routes create the 7 CRUD actions by default following REST.
resources :users
However, I have a confirm_destroy action that I use in almost every resource, because I have a lot of logic that goes on the confirmation page; it's not just a simple yes/no alert dialog.
resources :users do
get :confirm_destroy, on: :member
end
With 50+ resources, it gets tedious to write this out for each resource and my routes file is literally 3x longer because of this.
Is there any way to add an action to the standard 7 for the resources block such that
resources :users
would be the same as
resources :users do
get :confirm_destroy, on: :member
end
and I can use it in the routes as a standard action, ie:
resources :users, only: [:show, :confirm_destroy, :destroy]
resources :users, except: [:confirm_destroy]
While not quite as elegant as you might like, the Rails way would be to use a routing concern, as #dbugger suggested.
For example:
concern :confirmable do
get 'confirm_destroy', on: :member
end
resources :users, :widgets, concerns: :confirmable
$ rails routes
Prefix Verb URI Pattern Controller#Action
confirm_destroy_user GET /users/:id/confirm_destroy(.:format) users#confirm_destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
confirm_destroy_widget GET /widgets/:id/confirm_destroy(.:format) widgets#confirm_destroy
widgets GET /widgets(.:format) widgets#index
POST /widgets(.:format) widgets#create
new_widget GET /widgets/new(.:format) widgets#new
edit_widget GET /widgets/:id/edit(.:format) widgets#edit
widget GET /widgets/:id(.:format) widgets#show
PATCH /widgets/:id(.:format) widgets#update
PUT /widgets/:id(.:format) widgets#update
DELETE /widgets/:id(.:format) widgets#destroy
Im trying to learn to dry up my code a bit but have come across a issue,
Im scoping my routes with controller and path options
scope path: '/administrators', controller: :administrators do
get 'unverified' => :unverified
patch 'verify/:id' => :verify
get 'reported' => :reported
get 'ban_user' => :ban_user
patch 'execute_ban/:id' => :execute_ban
end
So this is what ive done so far, all the get links are working correctly...
but this is making the patch 'execute_ban/:id' => :execute_ban become a extension of ban user like this: (also the same with verify)
verified GET /administrators/unverified(.:format) administrators#unverifie
PATCH /administrators/verify/:id(.:format) administrators#verify
reported GET /administrators/reported(.:format) administrators#reported
ban_user GET /administrators/ban_user(.:format) administrators#ban_user
PATCH /administrators/execute_ban/:id(.:format) administrators#execute_ban
now ive changed my link_to route = link_to 'ban', ban_user_path(x.id), method: :patch
but its throwing an routing error saying no path matches.
Is there something im missing, any insight would aooreciated as always.
Thanks
Why not go for a more restful design that centers around the resource being modified?
Rails.application.routes.draw do
resources :users do
get :unverified, on: :collection
get :reported, on: :collection
get :ban
patch :ban, action: 'execute_ban'
end
end
You don't need different paths for the form and acting on a resource - use the HTTP verb instead.
Prefix Verb URI Pattern Controller#Action
unverified_users GET /users/unverified(.:format) users#unverified
reported_users GET /users/reported(.:format) users#reported
user_ban GET /users/:user_id/ban(.:format) users#ban
PATCH /users/:user_id/ban(.:format) users#execute_ban
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
If you are adding a separate controller just for authorization purposes it's an anti-pattern of sorts since you are just adding more complexity for something that should be handled by Pundit/CanCanCan.
I'm working on supporting users in my Rails app. There's no need for a user to even be aware that there are other users out there. I don't want to just use resources :users, because the routes that generates are these:
users GET /users users#index
POST /users users#create
new_user GET /users/new users#new
edit_user GET /users/:id/edit users#edit
user GET /users/:id users#show
PATCH /users/:id users#update
PUT /users/:id users#update
DELETE /users/:id users#destroy
(.:format) removed for improved readability.
You'd have to put the user id number in the URL, and that offers a chance for users to be aware that other users exist. I want these routes:
users GET /users users#index
POST /users users#create
new_user GET /users/new users#new
edit_user GET /users/me/edit users#edit
user GET /users/me users#show
PATCH /users/me users#update
PUT /users/me users#update
DELETE /users/me users#destroy
Yes. /users/me is the path of your user, and that's the only user path you can get at.
But the problem is defining these routes. Here's one idea:
resources :users, constraints: { id: 'me' }
And in the User model:
def to_param
'me'
end
But that seems too kludgy for me. Any better ideas?
With Singular Resources your are good to go ;-)
Define your routes like this:
# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :user, path: '/users/me', only: [:show, :edit, :update, :destroy]
Your routes will be singular (/user) if you leave the path: option. Play around with the options ;-)
And your rake routes result should look like this:
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/me/edit(.:format) users#edit
user GET /users/me(.:format) users#show
PATCH /users/me(.:format) users#update
PUT /users/me(.:format) users#update
DELETE /users/me(.:format) users#destroy
You can also test the routes on the Rails console (rails c)
2.1.3 :001 > Rails.application.routes.url_helpers.user_path
=> "/users/me"
2.1.3 :002 > Rails.application.routes.url_helpers.edit_user_path
=> "/users/me/edit"
If you want, you can pluralize :user on the Singular Resources, but don't forget to set the as: option, her an example:
# config/routes.rb
resources :users, only: [:index, :create, :new]
resource :users, path: '/users/me', as: 'user', only: [:show, :edit, :update, :destroy]
Please take a look to the notes and warnings in the Rails guide! Here is an excerpt:
A long-standing bug prevents form_for from working automatically with singular resources. ...
Lets say I have an UsersController that contains an action #new. In my routes file I map with the following:
match 'signup', to: 'users#new'
This action can now be accessed by both /signup and /users/new. How do I restrict it to only the custom route.
I apologize if this has been answered, but am new to this. I've searched, but haven't found the answer. Possibly due to my not knowing how to concisely phrase this.
You can exempt the new route from the users resource, and replace it with your custom route:
resources :users, except: [:new]
get 'signup', to: 'users#new', as: "new_user"
Resulting in:
users GET /users(.:format) users#index
POST /users(.:format) users#create
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
new_user GET /signup(.:format) users#new
So I've been working on a fairly simple rails 4 app and I've reached the point where I need to add user authentication. In particular, I want to use Google Apps (and only google apps) authentication via a combination of devise and omniauth. Now, devise has a tutorial that supposedly tells you how to set something like that up. After installing devise and making the suggested changes in the tutorial, everything seemed great. I clicked my sign-in link and was properly sent off to google for authentication. However, after I supply my credentials I'm immediately greeted with a routing error:
uninitialized constant Users
Which is confusing. From what I understand, that means that the controller is missing... but I definitely have users_controller.rb and it's where it should be. Barring that, I have no clue.
Here's my route.rb for reference:
resources :instances, :users
devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks' }
# authentication routes
devise_scope :user do
get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
root to: 'instances#index'
And here's the result of rake routes:
Prefix Verb URI Pattern Controller#Action
instances GET /instances(.:format) instances#index
POST /instances(.:format) instances#create
new_instance GET /instances/new(.:format) instances#new
edit_instance GET /instances/:id/edit(.:format) instances#edit
instance GET /instances/:id(.:format) instances#show
PATCH /instances/:id(.:format) instances#update
PUT /instances/:id(.:format) instances#update
DELETE /instances/:id(.:format) instances#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/google_apps/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:google_apps)
new_user_session GET /sign_in(.:format) devise/sessions#new
destroy_user_session GET /sign_out(.:format) devise/sessions#destroy
root GET / instances#index
Also of note is that I'm running rails 4 and devise 3.0.0.rc (because it's rails 4 compatible)
Let me know if there's anything else you need, pretty much everything else that is relevant is in the tutorial thing though.
You're problem lies here: :omniauth_callbacks => 'users/omniauth_callbacks'
'users/omniauth_callbacks' translates to Users::OmniauthCallbacksController. While your application does have a User model and a UserController, you haven't declared a constant which defines a Users namespace.
You'll need to add a controller in that namespace to handle the callback:
# app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
# action names should match the names of the providers
def facebook; end
def twitter; end
def github; end
...
end