I'm playing on Rails routing but I can't figure out how to manage this. The problem is at user_posts(1) and user(4) routes, the params have different names - id and user_id
What I'm trying to achieve is a static param name for the same resource.
I have this route file
Rails.application.routes.draw do
shallow do
resources :users, module: :users, only: [:index, :show] do
resources :posts, module: :posts, only: [:index, :show]
end
end
end
The routes generated are
user_posts(1) GET /users/:user_id/posts(.:format) users/posts/posts#index
post(2) GET /posts/:id(.:format) users/posts/posts#show
users(3) GET /users(.:format) users/users#index
user(4) GET /users/:id(.:format) users/users#show
I tried resources :user param: :user but the generated route at user_posts is /users/:user_user_id/posts
Is possible to achieve a :user_id param and :post_id param for every route using resources?
Try this
resources :users, param: :user_id
resources :users, only: [] do
resources :posts, param: :post_id
end
It is not possible 'cause it is useless. Your only option is to nest post into the user resources in such manner:
resources :users, only: [:index, :show] do
resources :posts, only: [:index, :show]
end
Related
In my routes.rb file, I have the following resources
resources :classrooms, only: [:index, :new, :create] do
resources :students, only: [:index, :edit, :update], controller: 'students' do
get :honor
end
end
My honor route becomes /classrooms/:classroom_id/students/:students_id/honor(.:format)
but I want to replace student_id to id so that I can easily use it in the students controllers.
Ideal route is /classrooms/:classroom_id/students/:id/honor(.:format)
You can do that specifying it's a member route. this way
get :honor, on: :member
or
member do
get :honor
end
In my rails 6 project (development mode) i've set up a User model along with devise.
In routes.rb i have:
scope :auth do
devise_for :users
end
resources :users, except: :index
resources :articles, only: [:show, :update]
scope :passport do
resources :users, only: :index
resources :articles, except: [:show, :update]
end
Prefixes work as expected for articles but not for users.
For some reason /passport/users refering to users#index
doesn't get the users_path prefix (GET) at all.
It's without any prefix.
Request to localhost:3000/passport/users works fine.
Is there a conflict with devise?
Im missing something but what is it?
Problem not in device gem
You can try this
scope :auth do
devise_for :users
end
scope :passport do
resources :users, only: :index
resources :articles, except: [:show, :update]
end
resources :users, except: :index
resources :articles, only: [:show, :update]
I don't know why, but it works
I've the following nested route:
resources :carts, only: [:show, :update, :create], param: :token do
resources :items, :controller => :cartitems, except: [:new, :edit], param: :product_id
end
This generates
GET /api/merchants/:merchant_id/carts/:cart_token/items(.:format) cartitems#index
POST /api/merchants/:merchant_id/carts/:cart_token/items(.:format) cartitems#create
GET /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#show
PATCH /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#update
PUT /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#update
DELETE /api/merchants/:merchant_id/carts/:cart_token/items/:product_id(.:format) cartitems#destroy
...
I want to remove the controller part in the resources :items parameters. That means: I want to rename the :cart_token parameter to :token. Just like:
GET /api/merchants/:merchant_id/carts/:token/items(.:format) cartitems#index
POST /api/merchants/:merchant_id/carts/:token/items(.:format) cartitems#create
GET /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#show
PATCH /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#update
PUT /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#update
DELETE /api/merchants/:merchant_id/carts/:token/items/:product_id(.:format) cartitems#destroy
...
How can this be achieved?
Don't know if it's the best way to do that, but I got the routes you expected there by:
scope 'api/mechantes/:merchant_id/carts/:token' do
resources :items, :controller => :cartitems, except: [:new, :edit], param: :product_id
end
Hope this can helps. Good luck!
Need to generate the following routes as
/:owner_name/articles
/:articles/:id
/owners
/:owner_name
I have tried as,
resources :owners, param: :owner_name, path: '', shallow_path: "", only: [:index, :show] do
resources :articles, only: [:index,:show]
end
But I am not able to reproduce the same
Maybe it is not the best solution but you can try the following
resources :articles, only: :show
resources :owners, only: :index
resources :owners, param: :name, path: '', only: :show do
resources :articles, only: :index
end
It produces
Prefix Verb URI Pattern Controller#Action
article GET /articles/:id(.:format) articles#show
owners GET /owners(.:format) owners#index
owner_articles GET /:owner_name/articles(.:format) articles#index
owner GET /:name(.:format) owners#show
UPDATE
The code above has :name parameter instead of :owner_name for owners#show route (if it does matter for you).
You can try to specify parameters manually like
resources :articles, only: :show
resources :owners, only: :index
get '/:owner_name', to: 'owners#show'
get '/:owner_name/articles', to: 'articles#index'
It will give you
Prefix Verb URI Pattern Controller#Action
article GET /articles/:id(.:format) articles#show
owners GET /owners(.:format) owners#index
GET /:owner_name(.:format) owners#show
GET /:owner_name/articles(.:format) articles#index
I have the following route:
resources :articles, except: [:index] do
collection do
get 'author/:id/articles' => 'articles#index', as: 'index_articles'
end
end
This results in:
GET /api/v1/articles/:id/author/:id/articles(.:format)
How can I turn this into the following?:
GET /api/v1/author/:id/articles(.:format)
# config/routes.rb
resources :articles, except: [:index]
resources :authors, path: "author", only: [] do
resources :articles, only: :index, on: :member #-> url.com/author/:id/articles
end
Write this
get 'author/:id/articles' => 'articles#index', as: 'index_articles'
Outside the resources :articles block.