how can I generate a RoR route like this one:
Prefix: gateway
verb: post
URI Pattern: /region/:id/gateway
Controller#Action: region#gateway
I tried with
resources :region, :only => :show do
post :gateway, :only => :show
end
but is generating by default a route to show the regions
/region/:id(.:format)
I want to skip the regions show action and only keep it the /region/:id/gateway route
post "gateway/region/:id/gateway" => "region#gateway"
Have a look to Rails routing documentation. It's well documented.
If You want to use resources, You may write:
resources :region do
member do
post :gateway
end
end
It will be recognized as:
region/:id/gateway
You can't use "only" because it reduce to one action!
Related
In rails 4.2 I have a controller that I want to fill with non resourceful routes something like
class TwilioController < ApplicationController
def add_to_queue
end
def another_action
end
end
I then want to access theese actions like so
http://appdomain/twilio/add-to-queue
and
http://appdomain.com/twilio/another-action
I realise I could do this like so in the routes file
get 'twilio/add-to-queue', to: 'twilio#add_to_queue'
get 'twilio/another-action', to: 'twilio#another_action'
but is there a way of grouping all of them together so I don't have to explicitly add twilio at the beginning of each route.
Ok I have figured out a solution, it seems quite a succinct one.
scope path: 'twilio', as: 't' do
get 'add-to-queue', to: 'twilio#add_to_queue'
end
So i now have routes like:
t_add_to_queue GET /twilio/add-to-queue(.:format) twilio#add_to_queue
Here's my solution that works in Rails 3.2.
scope :path => :twilio, :controller => :twilio, :as => :twilio do
get :add_to_queue
get :another_action
get :yet_another_action
end
:path adds \twillio\... to the URL
:controller maps all the nested routes to TwillioController
:as appends the URL helpers with twillio_....
In my routes.rb I had this resource
resources :home_screen_buttons do
post :update_multiple, :on => :collection
end
update_multiple helper is update_multiple_home_screen_buttons
Then I decided to remove resource because i need only update_multiple method in my controller, so I changed routes.rb to
post "home_screen_buttons/update_multiple"
It create helper home_screen_buttons_update_multiple instead of update_multiple_home_screen_buttons
Why it has different routing helper name?
It makes sense that :on => :collection has different helper then :on => :member, but is there any other way then adding :as => :update_multiple_home_screen_buttons to my post method for same behavior?
This is how Rails does this. When match is used, it maps the URI to the action and creates the corresponding helper path as controller_action_path
but when it is used as collection, that becomes RESTful action for that resource and Rails gives it a logical name relating to a collection. As quoted as an example here:
resources :photos do
collection do
get 'search'
end
end
generates search_photos_path.
You could have done this:
resources :home_screen_buttons, :only => [:update_multiple] do
post :update_multiple, :on => :collection
end
When defining a resources in routes.rb in Rails, I have the following problem: My resource supports the standard CRUD operations and has a custom function/route, which allows filtering. Now this custom routes matches the edit route and jumps in before the actual RESTful route.
Is there a way to prioritize the RESTful routes so that they match first?
resources :items do
get ':category(/:level)', :action => :filter, :on => :collection, :as => 'filter'
end
You should just set a simple get route ( if it is a GET request )
get 'filter', :to => "items#filter"
If you have any problems there are always RoR Guides :)
http://guides.rubyonrails.org/routing.html
I want to have an endpoint to destroy an instance of a model through a POST action like the Twitter API does:
statuses/destroy/:id
How would I define this route in the route file? I'm at a loss.
it may look better if you have other actions in the same resource in rails 3
resources :statuses, :only => [:index, :create]
collection do
post 'destroy/:id', :action => :destroy
end
end
I'm not sure if this is what you're trying to do but add this to your config/routes.rb should do the trick:
match 'statuses/destroy/:id' => 'statuses#destroy', :via => :post
(I've found it in the Rails Routing Guide)
I am migrating a Rails 2.3.8 version to Rails 3.0 and so ive rewritten my routes file. When i list the routes using rake routes, i see some route names have _index appended to them. I cant figure out why this is.
Relevant routes:
Rails 2.3.8:
map.namespace "tracker", :path_prefix => "" do |planner|
planner.resources :planner, :collection => {:step1 => :get,
:add => :get,
:unsubscribe => [:get, :post] }
end
Rails 3.0 route:
namespace "tracker", :path => "" do
resources :planner do
collection do
get :step1
get :add
get :unsubscribe
post :unsubscribe
end
end
end
Output from rake routes
Rails 2.3.8
step1_tracker_planner GET /planner/step1(.:format)
add_tracker_planner GET /planner/add(.:format)
unsubscribe_tracker_planner GET /planner/unsubscribe(.:format)
POST /planner/unsubscribe(.:format)
Rails 3.0
step1_tracker_planner_index GET /planner/step1(.:format)
add_tracker_planner_index GET /planner/add(.:format)
unsubscribe_tracker_planner_index GET /planner/unsubscribe(.:format)
POST /planner/unsubscribe(.:format)
Any ideas as to why this _index is being added would be much appreciated.
It is because your resource is named :planner instead of :planners that Rails decided to add the _index to any collection nested underneath. My guess it is there for readability.
The action named in the collection normally translates to a verb, so I can see why this makes sense. Take the typical photos resource example given in the routing docs:
resources :photos do
collection do
get 'search'
end
end
search_photos GET /photos/search(.:format)
But if instead we called the resources 'photo'...
resources :photo do
collection do
get 'search'
end
end
search_photo_index GET /photo/search(.:format)
In the first case, you search the "photos", and in the second case you search the "photo index".
You should be using either resource :planner either resources :planners depending on what you need. To learn about singular resource and it differences check out Rails Guides.
Following on from Semyon Perepelitsa's response, note that resource :planner expects the controller's name is PlannersController, whereas resources :planners expects PlannerController.
If you don't want to rename your controller when changing from resources to resource, you can override the default by specifying resource :planner, controller: :planner.