Rails 4 shallow routes menu parameters undefined error - ruby-on-rails

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)

Related

Rails 6 routing for admin section with arguments in the URL

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...

Uninitialized constant Admin::Admin

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

Nested routes and url helper in Rails 4

I have nested routes which goes something like this:
resources :venues do
#Halls
get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
get "hall/:id/visit" => "halls#visit", as: :hall_visit
get "venue_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 approach is that I have to put in three paramters in url helpers for nested ones like below:
venue_hall_booth_path(#booth.hall.venue, #booth.hall, #booth)
Is there a better approach to doing this other than me having to put in three different resources as parameters each time when I use this helper?
You can use shallow routes:
resources :halls, shallow: true do
resources :webcasts
resources :booths do
resources :chats
end
end
This allows you access member urls without having to use the parent. Unless it's a new or create actions.
Or you can define them separately.
resources :booths do
resources :chats
end
resources :halls do
resources :webcasts
resources :booths
end

Rails: Can't use member outside resource(s) scope (ArgumentError)

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.

Simple Rails routing, doing a get 'action' inside resource block

Consider this in routes.rb
resource :places do
resource :people
get 'search'
end
When I do this in browser:
localhost:3000/places/search
It gives me invalid id for place error.
I'm looking to do this without using a "match"
You need to specify that the get applies to the collection:
resource :places do
resource :people
get 'search', :on => :collection
end
See the documentation on collection routes for details.
resources :places do
resources :people
get 'search', :on => :collection
end

Resources