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...
Related
Using Ruby: 2.3.1p112 and Rails: 3.2.12
I'm trying call a demo method in my controller. So, in my _form.html.erb I have:
<%= link_to 'Demo', "/admin/clinics/"+#clinic.id.to_s+"/demo" %>
In my routes.rb:
match "/admin" => "admin#index", :as => :admin
namespace :admin do
resources :admin_users
resources :health_plan_tables
resources :health_aid_tables
resources :clients
resources :clinics
resources :specialties
resources :qualifications
resources :profissionals
resources :addresses
resources :documents
resources :banners
root :to => 'banners#index'
get 'logout' => 'devise/sessions#destroy'
get 'clinics/:id/demo', to: 'admin/clinics#demo', as: 'demo'
end
My clinics_controller.rb is inside the folder controllers/admin, and I just have:
def demo
print "hello"
end
So, when I click on link, error message appears Uninitialized constant Admin::Admin.
Any ideia how to fix it?
Since you are already defining your demo route inside a namespace there is no need to specify admin/clinics#demo, just clinics#demo will be necessary:
namespace :admin do
resources :admin_users
resources :health_plan_tables
resources :health_aid_tables
resources :clients
resources :clinics
resources :specialties
resources :qualifications
resources :profissionals
resources :addresses
resources :documents
resources :banners
root :to => 'banners#index'
get 'logout' => 'devise/sessions#destroy'
get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
end
According to the error log you're looking for a controller namespaced under admin/admin/clinics (it says that in the controller part of the params).
Change the bottom route to not include admin (it's already namespaced and you're effectively namespacing it twice):
get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
This will route to the correct controller, admin/clinics, instead of admin/admin/clinics
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
I have shallow routes like below:
resources :venues, shallow: true do
#Halls
get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
get "hall/:id/visit" => "halls#visit", as: :hall_visit
get "structure", :to => "venues#venue_structure"
resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
resources :halls do
resources :webcasts
resources :booths do
resources :chats
end
end
end
Problem with this is with below helper where I need to provide #booth resource as a parameter:
hall_booths_path(#booth.hall)
But this is not always possible especially when it gets to index action as it does not set #booth resource.
Is there a good approach to tackle this problem?
Even if you're in booths#index action, you have your hall id, so you could set #hall instance variable:
#hall = Hall.find(params[:hall_id])
and then, you just can do following:
hall_booths_path(#hall)
I have been upgrading to Rails 4, and after running command "rspec ." I am getting the following error:
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/actionpack-4.0.0.rc1/lib/action_dispatch/routing/mapper.rb:1297:in `member': can't use member outside resource(s) scope (ArgumentError)
I have changed my route file a little and still no work. If anyone has run into a similar issue and has a fix please share with me. Thanks in advance!
Dating::Application.routes.draw do
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'logout' => 'sessions#destroy'
get 'edit' => 'users#edit'
get "/profile/:id" => "users#show"
get "profile/:id/settings" => 'users#edit'
get 'settings/:id' => 'users#settings'
resources :sessions
resources :password_resets
resources :galleries
resources :photos
resources :searches
resources :questions do
resources :answers, only: [:new, :create]
end
resources :users do
member do
get :settings
end
end
root to: 'galleries#index'
resources :users do |user|
resources :messages do
collection do
post 'delete_multiple'
get 'settings', on: :member
end
end
end
I think the problem is here:
resources :messages do
collection do
post 'delete_multiple'
get 'settings', on: :member # <--here
end
end
You can't define a member inside the collection, you should move it up to the resources block, like this:
resources :messages do
collection do
post 'delete_multiple'
end
get 'settings', on: :member
end
Remember that a member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects.
So you can't use both at the same time.
Hello im doing tutorial from Rails Book and get into some troubles.
While im trying to log in with my username and password im getting the following error:
Routing Error
No route matches [POST] "/sessions/new"
Try running rake routes for more information on available routes.
This is my route file config.
ZomfgShop::Application.routes.draw do
get "admin/index"
get "sessions/new"
get "sessions/create"
get "sessions/destroy"
resources :users
resources :orders
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
resources :products
root to: 'store#index', as: 'store'
resources :line_items do
#member do
# post 'decrement'
#end
post 'decrement', on: :member
end
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
You have controller instead of resources for the :sessions routes.
resources :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
Try replacing
get "sessions/new"
get "sessions/create"
get "sessions/destroy"
with
resources :sessions
make sure you have a views/sessions/new.html.erb file (under views/devise/ if using devise)
and make sure you have this in sessions_controller.rb if you aren't using Devise
def new
end
def create
end
def destroy
end
Thank you guys, changed my routes.rb file to this and my routing fine is working fine once again!
ZomfgShop::Application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
scope '(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
resources :line_items do
#member do
# post 'decrement'
#end
post 'decrement', on: :member
end