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!
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
I’ve this on routes.rb
resources :questions, except: [:show] do
get '/resource/:subject/:id', to: 'resource#show', as: "resource", param: [:name, :id]
It says that:
Invalid route name, already in use: ‘resource' You may have defined two routes with the same name using the :as option, or you may be overriding a route already defined by a resource with the same naming
I know that resources create two routes with the same path, show and destroy both uses resource_path, how does it is being created internally? and how i can generate my route for show wihtout overwrite the one in destroy?
A good way to eliminate routes unneeded is by specifying the :only option
resources :user, :only => [:edit]
instead of
resources :user, :except => [:new, :create, :edit, :update, :show, :destroy]
It seems to me that you could take out show and then define the route that you want separately. See if this works:
resources :questions, except: :show
get '/resource/:subject/:id',
to: 'resource#show',
as: "resource", # This is where the error is.
param: [:name, :id]
EDIT: Ah, yes. The :as parameter needs a different name. This will work:
resources :questions, except: :show
get '/resource/:subject/:id',
to: 'resource#show',
as: "resource_show",
param: [:name, :id]
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
I'm using Wicked to build an object in steps, and would like to clean up my routes a little bit.
Currently, my router looks like this:
resources :surveys, only: [:new, :create], path: 'feedback' do
resources :steps, only: [:show, :update], controller: 'survey/steps'
end
So my routes end up being:
GET '/feedback/new' => 'surveys#new'
POST '/feedback/create' => 'surveys#create'
GET '/feedback/:id/steps/step1' => 'survey/steps#show'
PUT '/feedback/:id/steps/step1' => 'survey/steps#update'
Ideally, I'd like to remove both the survey id and the 'steps' name from my routes, so that they look like this:
GET /feedback => 'surveys#new'
POST /feedback => 'surveys#create'
GET /feedback/step1 => 'survey/steps#show'
PUT /feedback/step1 => 'survey/steps#update'
...
Any simple way to do this?
resource :steps #as singular
resources :surveys, only: [:new, :create], path: 'feedback' do
resource :steps, only: [:show, :update], controller: 'survey/steps'
end
I am trying to add a few new pages to a rails resource that I am creating.
What I am doing in my routes file is as follows:
resources :users, :only => [:index, :show] do
collection do
get :show_subpage1
end
end
When I look at my routes, show_subpage1 shows up, but not in the format I want. What shows up in the routes is:
show_subpage1_users GET /users/show_subpage1(.:format)
When what I WANT the route to be is:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
(with the ID).
How would I go about doing that in rails?
To get:
show_subpage1_users GET /users/:id/show_subpage1(.:format)
do not define :show_subpage1 as a collection route:
resources :users, :only => [:index, :show] do
get :show_subpage1
end
or you could define it as a member route as follows:
resources :users, :only => [:index, :show] do
member do
get :show_subpage1
end
end
Also, I'm unsure why you have :only => [:index, :show] defined if you are also going to have a member route :show_subpage1. I assume you do want to add add :show_subpage1 to the only array, i.e. resources :users, :only => [:index, :show, :show_subpage1] do.
Please take a read on "Adding More RESTful Actions"
there are two ways with resources member or collection
resources :users, :only => [ :index, :show ] do
# /users/:id/profile
get 'profile', :on => :member
# /users/profile
get 'profile', :on => :collection
end
hope this helps