i have a problem as following.
i have a resources setup in my routes.rb file as following
resources :users do
resources :sub_transactions
end
resources :sub_transactions do
collection do
get :income
get :expenditure
end
end
Now what is the correct route that i should write so that i can generate the following routes
users/1/sub_transactions/income
users/1/sub_transactions/expenditure
where income and expenditure are not ids
Have you tried adding those calls to the nested sub_transactions resource? Like:
resources :users do
resources :sub_transactions do
collection do
get :income
get :expenditure
end
end
end
I'm not in a position to test at the moment but that would be the logical starting point (I figure).
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 have two controller Stores and Stocks and routes for these two controller is given below:
resources :stores do
resources :stocks,param: :product_id,:only=>[:index] do
get '/:product_id', to: 'stocks#index'
end
end
After rake routes I'm getting the path like:
GET /stores/:store_id/stocks/:stock_id/:product_id(.:format)
But I want to remove :stock_id from that path so that the resultant path will be:
GET /stores/:store_id/stocks/:product_id(.:format)
If it's possible then please help.
Try this:
resources :stores do
resources :stocks, except: :index do
get '/:product_id', to: 'stocks#index', on: :collection
end
end
This will give you:
GET /stores/:store_id/stocks/:product_id(.:format) stocks#index
if you want default index action also then:
resources :stores do
resources :stocks do
get '/:product_id', to: 'stocks#index', on: :collection
end
end
This will give you both index:
GET /stores/:store_id/stocks/:product_id(.:format) stocks#index
store_stocks GET /stores/:store_id/stocks(.:format) stocks#index
Here's my problem:
I have a web app, in which users can create posts.
User and post are created simultaneously - I extract the user's email from the post to create his user entry. (No password/login/registration etc required)
In my routes.rb file, I have posts nested with users (see attached)
Now, here is my question:
Where should the posts#new creation form be? Currently I have it at /posts/new but this is clearly wrong, I am getting a routing error.
Grateful for any feedback.
routes.rb
Mysalary::Application.routes.draw do
resources :users do
resources :posts
end
resources :profiles
resources :pages
get "pages/home"
get "pages/about"
get "pages/legal"
get "pages/feedback"
root :to => 'posts#new'
end
I would add posts on it own, so to have both you would have:
routes.rb
resources :users do
resources :posts
end
resources :posts
You have posts only as a nested resource, so you would find it at /users/:user_id/posts/new
If you want to reach it at /posts/new, just un-nest resources :posts. You can also leave it nested and repeat it outside the nesting, then it would be reachable both ways.
Remember to run rake routes in the console.
I have a nested resource that looks like this:
resources :events
resources :attendances
post 'update_email'
end
end
and it shows me routes that look like this (left out most of the standard REST routes for brevity):
event_attendance GET /events/:event_id/attendances/:id
event_attendance_update_email POST /events/:event_id/attendances/:event_attendance_id/update_email
So, why is it that when I add new routes, they have a different id parameter?
Ack, figured this out almost immediately after posting.
The problem is that I didn't specify that it was a route for a member resource:
resources :events
resources :attendances
member do
post 'update_email'
end
end
end
produces what I wanted:
event_attendance GET /events/:event_id/attendances/:id
event_attendance_update_email POST /events/:event_id/attendances/:id/update_email