Rails4 Should I prevent multiple URIs indicate the same action? - ruby-on-rails

Suppose you have UsersController and routes are configured in config/routes.rb as the followings.
root 'users#index' # root should list all users
resources :users
Then rake routes shows
Prefix Verb URI Pattern Controller#Action
root GET / users#index
users GET /users(.:format) users#index
users 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
There are a couple of URIs indicating same users#index action.
Should I prevent this kind of URI duplication or should I let them alone for me to follow Rails' default convention?

There are a couple of URIs indicating same users#index action. There are a couple of URIs indicating same users#index action.
Well, that's what you've specified by creating the users resource and then pointing the root path to users#index. If you don't want this behavior, you can remove the default index action from the resource.
resources :users, except: [:index]
Note that with this place, you can no longer refer to the index path via the traditional URL helpers. Personally I wouldn't be concerned with this and just let the URL duplication exist.

For root it's okay--this is typical and it's just redirecting to the users resource.

Related

Scoped routes creating a routing issue

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.

Is it possible to change params through web?

I am coding a website, that have no legit option for editing users.
Once created, nothing can be changed.
And here is my question:
Is it possible to change users data (for example by sending patch requests) without using edit form (because I did not create one), by 3rd party (a.k.a. not host)?
As Rails 5 requires, I am using strong params (sending user name and password when creating user by signup form).
#routes
Prefix Verb URI Pattern Controller#Action
root GET / sessions#new
signup GET /signup(.:format) users#new
POST /signup(.:format) users#create
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#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
Should also mention that there is no update method, but I am using
resources :users
For third party access to your db you probably need an API.
Simplified example:
You app's URL is https://myapp.example.com/.
config/routes.rb:
namespace :api do
namespace :v1 do
resources :users, only: [:update]
end
end
rake routes:
Prefix Verb URI Pattern Controller#Action
api_v1_user PATCH /api/v1/users/:id(.:format) api/v1/users#update
PUT /api/v1/users/:id(.:format) api/v1/users#update
app/controllers/api/v1/base_controller.rb:
class Api::V1::BaseController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
protect_from_forgery with: :null_session
end
app/controllers/api/v1/users_controller.rb:
class Api::V1::UsersController < Api::V1::BaseController
def update
# check params, maybe look for some security tokens
user = User.find(params[:id])
if user
user.update_attribute(:name, params[:name])
render plain: "success" and return
else
render plain: "failure" and return
end
end
end
Then request like this
curl -X PATCH https://myapp.example.com/api/v1/users/1?name=new_name
would change users' with id = 1 name by "new_name".
For proper API desing you may check http://jsonapi.org/.

Unable to route to a method in my controller

I’m using Rails 4.2.5. I’m trying to set up my controller so that when a logged in user visits /users/edit, they see my form where they can edit some of their profiles. So in config/routes.rb I have
resources :users
…
get "users/edit" => "users#edit"
then in “app/controllers/users_controller.rb” I have
def edit
#user = User.find(session["user_id"])
render 'edit'
end
but when I visit “http://localhost:3000/users/edit” in a browser, I get the error
The action 'show' could not be found for UsersController
It is true I have no “show” method in my controller, but that is not where I want the user to go. I want them going to the edit method.
You are trying to go to the show action with this link:
http://localhost:3000/users/edit
You have this route for the show action:
GET /users/:id(.:format) users#show
(:id) is (edit)
Because you have defined first RESTful route:
resources :users
Which includes all routes listed below:
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
and then
get "users/edit" => "users#edit"
Rails always finds first match. In this case show action of RESTFul routes will be applied:
GET /users/:id(.:format) users#show
and the other route will be ignored.
Solution: Change the order of the routes. That way edit route will be applied first.
The problem is that you're mixing a resource route with your own edit method, and "Rails routes are matched in the order they are specified" so it's matching the resources show route users/:id and stopping there.
You need to move your edit route above the resource.
Alternatively, read the linked guide and see if you can add edit as a collection route to the resource, and except the resources edit method. You may also need to except the show method, but it's worth having a play and seeing what you come up with. Routing is an important aspect and worth time to understand.
remove get "users/edit" => "users#edit" from your routes, change to resources :users, only: [:edit] (or more if actions you need them), remove the render 'edit' from your controller (default action).
Visit http://localhost:3000/users/1/edit to see the edit page for user_id 1 (there can't be an edit page for all users, you have to specify the id)

Restricting Rails routes to actions

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

Making Rails routing beautiful

I have the following in my Rails routes.rb:
resource :sign_up, only: [:new, :create]
resources :users
get 'users/activate/:token' => 'users#activate', as: 'activate_user'
Which gives me the following routes:
Prefix Verb URI Pattern Controller#Action
sign_up POST /sign_up(.:format) sign_ups#create
new_sign_up GET /sign_up/new(.:format) sign_ups#new
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
activate_user GET /users/activate/:token(.:format) users#activate
I'd like to get rid of the get 'users/activate/:token' ... route and use nesting or scoping instead, though I can't figure it out. Is there a way to accomplish this?
Thanks!
You can set up a collection route for users:
resources :users do
collection do
get 'activate/:token', :action => :activate, :as => :activate
end
end
And it will give you routes like this:
Prefix Verb URI Pattern Controller#Action
activate_users GET /users/activate/:token(.:format) users#activate
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

Resources