How to create clean URL in rails nested resource? - ruby-on-rails

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

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

Rails routes.rb use get to add suffix to path

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

Aliasing routes in rails 4

If I have the following routes
resources :pages do
resources :sections
end
I get routes that look like this:
/pages = #index
/pages/:id = #show
/pages/:id/edit = #edit
...etc
How can I go about making it so that the url for the #show action of the pages controller looks like '/:id', without the '/pages/' prefix? should I exclude #show from resources :page & create a get route + alias for it separately? or is there a way to do it from inside the resources :page block? Thanks in advanced.
EDIT:
Changed it to:
resources :pages, except: [:show] do
resources :sections
end
get '/:id', to: 'pages#show'
& rerouting non-existing :ids' to 404 for now, let me know if there's a better solution. Thanks.
get '/:id', to: 'pages#show', as: 'page'
Make sure this is at the bottom of your routes.rb file, otherwise it is going to hijack requests to other routes.
This also gives you page_url and page_path helper methods. But to use them you must exclude show action from previous routes.
resources :pages, except: [:show]

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

Setting up routes in rails

When I put the following code in my routes config :
resources :users do
end
I get all the CRUD operations routes. i.e
/users/new
/users/:id/edit
and so on.
How do I configure routes so I get route like this :
/users/lookup/:search_query
And when users reaches this routes he/she should be taked to lookup method of my controller
I would do :
resources :users do
get :lookup, on: :collection
end
And I would pass the search_query as a parameter. With that you will be more flexible.
resources :users do
get '/lookup/:search_query' => 'users#lookup', on: :collection
end
resources :users do
end
match '/users/lookup/:search_query' => "users#lookup", :as => :user_lookup

Resources