I have a rails routes config like that, and when I run rake routes it show that, but I wanna the perfix is restore_link, how can I do it?
rake routes
restore_links POST /:uid/links/:id(.:format) links#restore
routes config
resources :accounts, path: '/', param: :uid, only: [:show] do
member do
resources :links do
collection do
post ':id', to: "links#restore", as: "restore"
end
end
end
end
resources :accounts, path: '/', param: :uid, only: [:show] do
member do
resources :links, path: 'restore_links' do # <========= `path` option
collection do
post ':id', to: "links#restore", as: "restore"
end
end
end
end
Related
I have a rails nested resource in my routing.
i.e
resources :users do
resources :accounts
end
resources :accounts
The listing operations of course will be:
GET /users
GET /users/:user_id/accounts
I want to get rid of the /users route but retain the /users/:id/accounts route.
Any idea how I can go about this? Thanks
Using except: [:index] not will restrict both routes. Thats a nonsenical claim that can easily be refuted by just running rails routes. None of the options for resources "trickle down" to nested calls.
resources :users, only: [] do
resources :accounts, only: :index
end
only: [] skips generation of all the "user" routes.
This will generate the routes:
Prefix Verb URI Pattern Controller#Action
user_accounts GET /users/:user_id/accounts(.:format) accounts#index
# ...
Note that the param key is :user_id and not :id. If you REALLY want to break the conventions you would need to do:
# don't do this - its stupid
scope '/users/:id', as: :user do
resources :accounts, only: :index
end
let set only: [] then rails routes will generate /users/:id/accounts as you want
resources :users, only: [] do
resources :accounts # , only: [:index] if you just only keep users/:id/accounts
end
# if you only want to get rid of GET /users
resources :users, except: [:index]
# if you mean you want to get rid all of /users routes (not just only GET /users) then comment above line
This is what scope is for
scope :users do
resources :accounts
end
Rails guides on routing
Or you can use these same two ways, use namespace with scope:
namespace :users do
scope ':user_id' do
resources :accounts
end
end
Use just only scope:
scope ':users/:user_id' do
resources :accounts
end
how can I organize routing for /admin section with arguments before the "/admin".
For example, /:country_id/:lang_id/admin
Path example, /ukraine/english/admin
I have tried:
scope path:"/:country_id/:lang_id/admin", :as => "admin" do
resources :cities, controller:'admin/cities'
but "admin_cities_path" create wrong internal links
<%= link_to city.title, admin_cities_path(city.id) %>
Rails returns the following UR:: /1/english/admin/cities
instead of:
/ukraine/english/admin/cities/1
Routes.rb
Rails.application.routes.draw do
devise_for :users
devise_for :views
get 'home/index', to: 'application#index'
root to: "home#index"
get '/:id/:lang_id/admin', to: 'admin/admin#index'
get '/admin', to: 'admin/admin#index'
scope path:"/:country_id/:lang_id/admin", :as => "admin" do
#namespace :admin do
resources :cities, controller:'admin/cities'
resources :comments do
member do
get 'approve'
get 'disapprove'
end
end
resources :countries
resources :industries
resources :products
resources :languages
resources :categories
resources :companies
end
#root "articles#index"
#get "/admin/countries", to: "countries#index"# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
# resources :countries do
# resources :languages
# end
resources :countries
get '/countries/', to: 'countries#index'
get '/languages/', to: 'languages#index'
get '/:id/', to: 'countries#show', as: 'localized_country'
get '/:id/:lang_id/', to: 'languages#show', as: 'localized_language'
#get '/:id/:lang_id/:category_id/', to: 'categories#show', as: 'localized_category'
get '/:id/:lang_id/:industry_id/', to: 'industries#show', as: 'localized_industry'
get '/:id/:lang_id/:industry_id/:category_id/', to: 'categories#show', as: 'localized_category'
get '/:id/:lang_id/:industry_id/:category_id/:city_id/', to: 'cities#show', as: 'localized_city'
get '/:id/:lang_id/:industry_id/:category_id/product/:product_id/', to: 'products#show', as: 'localized_product'
resources :languages, param: :land_id
resources :products
resources :industries
resources :categories, param: :category_id
resources :products do
resources :comments
end
end
I am just guessing what you want if its not satisfied then pls comments some code or more information to solve it.
resources :country, path: '' do
resources :language, path: '' do
namespace "admin" do
resource :cities
end
end
end
The above code will reporduce the path in following format.
new_country_language_admin_cities GET /:country_id/:language_id/admin/cities/new(.:format) admin/cities#new
edit_country_language_admin_cities GET /:country_id/:language_id/admin/cities/edit(.:format) admin/cities#edit
and so on...
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 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.
routes.rb
Rails.application.routes.draw do
root to: 'visitors#index'
resources :states do
resources :cities do
get 'listings'
end
end
end
I am looking to have my GET URL set up like:
../state.id/city.id/listings.id
I am using friendly_id so the urls will read like:
../OR/Portland/2011-ford-truck
Listing is it's own model (resource) too in this case. You will also need a resources for listing. If it only has a show action, you can limit it like this:
resources :states do
resources :cities do
resources :listings, only: [:show]
end
end