Say I have a "CleaningLogEntry", "SalesLogEntry", "ServiceLogEntry" and as so on.
I understand that I can namespace the routes and have them nested. That's good. But I want them to just say log within that namespace. Is it possible
such as
resources :facilities do
resources :cleaning_log_entries
end
gives
facilities/20/cleaning_log_entries
But I want
facilities/20/logs
This is just the same concept repeated, in case it wasn't clear with some brainstorming
resources :client do
resources :sales_log_entries
end
#From
client/20/sales_log_entries
#To
client/20/logs
resources :services do
resources :services_log_entries
end
#From
services/20/services_log_entries
#To
service/20/logs
Whoops!
Figured it out. Missed this on Rails Guides:
http://guides.rubyonrails.org/routing.html#overriding-the-named-helpers
You need to add the ":as => 'desired_name'" option for:
resources :facilities do
resources :cleaning_log_entries, :as => 'logs'
end
Related
Two quite similar routing settings really is confusing.
resources :authors do
resources :books
end
and
resources :authors do
member do
resources :books
end
end
As we all know, rails will generate the following routings :
writer_book GET /writers/:writer_id/books/:id(.:format) books#show
and
book GET /writers/:id/books/:id(.:format) books#show
How is this member option useful?
One can just not using member option and set params[:writer_id] in books_controller and be done with it right?
Does this will have a bad affect when the application gets bigger? What are the consequences?
The member and collection methods are meant to add additional RESTful actions to resources
resources :writers do
member do
post :favorite
end
collection do
get :unpublished
end
end
They are not intended for nesting resources
# bad
resources :writers do
member do
resources :books
end
end
# good
resources :writers do
resources :books
end
What are the consequences?
Using member here will result in the route
GET /writers/:id/books/:id(.:format)
Which means that the id param is ambigous! It could be either the id of the book or the author! Not good! Not using member would give us params[:writer_id] which we can use to fetch the parent record.
GET /writers/:writer_id/books/:id(.:format) books#show
See:
Rails Routing from the Outside In
I have the following in my routes file:
resources :exercises, shallow: true do
resources :questions do
resources :test_cases do
member do
post :run, to: 'test_cases#run'
end
end
end
end
get 'test_cases/test', to: 'test_cases#test'
My problem is with the test route, outside the resources. Checking the available routes I have what I want:
test_cases_test GET /test_cases/test(.:format) test_cases#test
However, if I call test_cases_test_url from a view I'm redirected to test_cases#show, no test_cases#test how it should be. This question is about the same problem in a different situation. I don't want to follow the accepted answer because if I put get 'test', to: 'test_cases#test' inside my resources :test_cases and outside member block I'll got the question_id in my route, and inside member block the test_case_id. I don't need these ids.
I have any option to get my desired route(test_cases/test) working?
i think you're matching on the test_cases#show method because it has the same pattern as the test_cases/test url and it comes before. Just move the test_cases/test get route to the top of your routes file
get 'test_cases/test', to: 'test_cases#test'
resources :exercises, shallow: true do
resources :questions do
resources :test_cases do
member do
post :run, to: 'test_cases#run'
end
end
end
end
Another way, which I'd recommended
resources :exercises, shallow: true do
resources :questions do
resources :test_cases do
member do
post :run
get :test
end
end
end
end
I'm trying to figure out how can i build this route in rails:
resources :cases do
resource :profile do
get 'regions', :to => "cases#regions"
end
end
This code will generate method case_regions_profile_path, but i want it upside down: case_profile_regions_path, is it possible using get 'rule'? I just want to point this path to a controller with specific action.
Its typo you have used resource instead of resources for profiles
resources :cases do
resources :profile do
get 'regions', :to => "cases#regions"
end
end
This generates:
case_profile_regions GET /cases/:case_id/profile/:profile_id/regions(.:format) cases#regions
Hi i have a rails app and i use different namespaces in it like user, admin etc.
My url's under user namespaces as
'/user/:controller_name' but i want to
use it like
'/library/:library_id/user/:controller_name'
Users has one-to-one relationships with libraries. And i can get current users library id.
when i tried to make it with path parameter in route like
namespace :user, path: "library/user" do
...
end
it is working but i couldnt get id.
Is it possible to do this?
This does not work?
namespace :user, path: "library/:library_id/user" do
...
end
I guess, this is wat you are looking for
resources :libraries do
namespace :users do
resources :resource_name
end
end
# Output for me
/libraries/:library_id/users/controller_name/new(.:format)
/libraries/:library_id/users/controller_name/:id/edit(.:format)
.
.
This should work for you:
resources :libraries do
member do
resources :user do
collection do
resources :controller_name
end
end
end
end
I'm using some routes with scopes, and some without. See below.
resources :cities
resources :categories
devise_for :clients
namespace :clients do
resources :account
resources :dashboard
resources :offers
end
scope "/:current_city" do
scope "/:current_category" do
match 'articles/last_articles' => 'articles#index_last_articles', :as => "last_articles"
resources :articles do
resources :comments
end
end
end
root :to => "home#index"
I am using that params :current_city and :current_category and it gives me a URL like
http://localhost:3000/warszawa/all/articles/last_articles when I'm accessing articles.
**PROBLEM**
I have now such a problem that if I click on a link_to cities_path or root_path, then it adds those two parameters to the URL as http://localhost:3000/?current_category=all¤t_city=warszawa.
I don't want these two parameters destroying the beauty of my URL :o(
The only way I found was to pass :current_city => nil, :current_category => nil for each link, but that's really heavy. I tried also the same but in my routes, which is working for normal resources, but not for namespaces, root_path, devise_for routes, and to be honest, that looks horrible in the routes.rb.
**QUESTIONS**
First I do not understand why these params are passed everywhere if I ask them only in the section with scope?!
Secondly, is there a way to make it work like I want or I should modify my routes?
I wish you understand my problem and if you have any comment or idea, please do not hesitate!
Thx