Custom route redirect to wrong action - ruby-on-rails

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

Related

Getting correct ID in rails route

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

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

Update a model column value by clicking a link ruby on rails

I have a servicebooking model and I am trying update its accept_booking column with a value of 1 when the following link is clicked in my servicebooking show view:
<%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>
I get the following error:
undefined method `acceptbooking_servicebooking_path' for #<#<Class:0x5a35e98>:0x5a2bbf0>
Below I have declared the route in routes.rb
get 'acceptbooking', to: 'servicebookings#acceptbooking'
I have the following acceptbooking method in my servicebookings controller:
def acceptbooking
render nothing: true
#servicebooking = Servicebooking.find(params[:id])
#servicebooking.update_attribute(:accept_booking, 1)
end
my Routes.rb
Appa::Application.routes.draw do
devise_for :users
devise_for :views
get "welcome/index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
get 'myevents', to: 'events#myevents'
get 'myvenues', to: 'venues#myvenues'
get 'myservices', to: 'services#myservices'
get 'make_available', to: 'services#make_available'
get 'myservicebookings', to: 'servicebookings#myservicebookings'
get 'acceptbooking', to: 'services#acceptbooking'
#match 'acceptbooking', to: 'servicebookings#acceptbooking', via: [:get, :post]
resources :events do
resources :comments
end
resources :users do
resources :events
end
resources :venues do
resources :comments do
collection do
post :venuec
end
end
end
resources :services do
resources :comments do
collection do
post :servicec
end
end
end
resources :servicebookings do
resources :services
end
end
Can anyone see where Iam going wrong here? Thanks guys.
Try this: get 'acceptbooking/:id', to: 'servicebookings#acceptbooking', as: 'acceptbooking_servicebooking'
With just the route definition (from your route file):
get 'acceptbooking', to: 'servicebookings#acceptbooking'
the name of the route will be acceptbooking so you should be using acceptbooking_path. But based on your <%= link_to 'Accept this booking', acceptbooking_servicebooking_path(#servicebooking) %>, it appears like you want a url like /servicebookings/:service_booking_id/acceptbooking. If this is the case then you should update your route definition to:
resources :servicebookings do
resources :services
# Move the following line here from the top.
get 'acceptbooking', to: 'servicebookings#acceptbooking'
end
And this will give you servicebooking_acceptbooking_path (Note the order of servicebooking and acceptbooking here).

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