Getting correct ID in rails route - ruby-on-rails

When I do a rake routes,
GET test/:test_id/associated_link(.:format)
GET test/(.:format)
POST test/(.:format)
GET test/new(.:format)
GET test/:id/edit(.:format)
PATCH test/:id(.:format)
PUT test/:id(.:format)
DELETE test/:id(.:format)
I need the first instance to be test/:id/associated_link
Routes file looks like
Rails.application.routes.draw do
resources :years
resources :mateirals
resources :people
resources :jobs
resources :test do
get 'associated_links'
end
root 'welcome#index'
resources :welcome, :companies, :positions

It should be a member route.
resources :test do
member do
get 'associated_links'
end
end

You can use a member route, which can be found in the docs. It will look like this:
resources :test do
member do
get 'associated_links'
end
end
or, if you only have a single member route, you can eliminate the block as such:
resources :test do
get 'associated_links', on: :member
end

If you want the URI Pattern to look like test/:id/associated_link, you need to change
resources :test do
get 'associated_links'
end
to
resources :test do
get 'associated_links', on: :member
end
Tested

Related

Custom route redirect to wrong action

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

Placing User ID's within Routes with Devise

I currently have a default devise set up and wanting to put the user id's within my logged in routes and leave the others alone such as Welcome,FAQ, About us etc.
Such as
www.example.com/user-id/offers
My Current Routes File
devise_for :users
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
resources :offers do
member do
put :tourcomplete
end
end
resources :categories, only: :show
root 'welcome#index'
get '/balance', to: 'balance#show', as: 'balance'
patch '/balance', to: 'balance#paypal', as: 'balance_paypal'
patch '/withdraw', to: 'balance#withdraw', as: 'balance_withdraw'
I can't find any docs on this and my previous answer to a similar question was very vague and not helpful.
Thanks
You will need to use a nested routes:
resources :users do
resources :offers do
member do
put :tourcomplete
end
end
end
so now your routes will be /users/:id/offers, run rake routes to check your routes.
if you want to exclude users then you will have to specify the route yourself:
match ':user_id/offers' => 'offers#index'

How to create clean URL in rails nested resource?

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

Adding a restful route like: /users/234/add_section

I already have:
resources :users
How can I add a route (for POST only) that will work like:
/users/2343/add_section
Is it possible to add this route inside of the resources users block?
resources :users do
end
Having add_section there is not very RESTful. Consider having a subresource section and doing POST "/users/123/sections/"
Something like:
resources :users do
member do
post 'add_section'
end
end
As I answered on your previous question
resources :users do
post :add_section, :on => :member
end

cannot generate routes

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

Resources