Rails multiple controller has same action, Can we write single line route? - ruby-on-rails

I have many controllers which has an action download_excel. Action generates xls file defined using axlsx gem.
Problem is many controllers have same action. Is there anyway to write a single line route in routes.rb file?
I tried.
match ':controller(/:download_excel)', :via => [:get]
and
resource "#{:controller}" do
get download_excel, on: :collection
end
and
[:countries, :pricetypes].each do |file|
resources "#{file}" do
collection do
get "#{file}"+'/excel' => "#{file}"+'#excel'
end
end
end
etc. Nothing worked.
How can I write dynamic routes. Want to DRY code in routes as much as possible.

Try This
match ':controller(/:action(/:id))', :via => [:get, :post]
I use it as a catch all route, but if you want to your app to be restfull you should use the resources routes

Related

Rails API Routing Issue

I have the following specified in my Rails Routes. I want to allow both GET and POST on this route, but whatever I try, it only allows the #index action, and doesn't access the #create action when a POST is requested.
match ':user_id/special_deals', to: 'special_deals#index'
I've tried this too:
match ':user_id/special_deals', to: 'special_deals#index', :via => [:get, :post]
I need the User ID to be specified first since people with access to the API can access multiple User's info.
It is working exactly as you asked it to do. If you want POST to routed to create action here are your route configs:
match ':user_id/special_deals', to: 'special_deals#index', :via => [:get]
match ':user_id/special_deals', to: 'special_deals#create', :via => [:post]
There are simpler ways of writing these but I just wanted to use the same format you wrote it. Please check this guide to know about them.
If you already have a User controller, you can write more structured routes like:
resources :users do
resources :special_deals, :only => [:index, :create]
end
This will make routes for special_deals like (#shows where it will be routed to):
GET /users/:user_id/special_deals #special_deals#index
POST /users/:user_id/special_deals #special_deals#create

Rails custom route not working

Feel like I'm doing this right, but apparently not.
I have a restful resource, Posts, with index, show, new, update, edit, etc actions in the controller. In routes, I have
resources :posts
I wanted to make the index action occur at the URL '/archive' instead of '/posts'
So I added this line in the routes.rb file, after the resources one:
match '/archive', to: "posts#index"
But when I click on a link to posts_path, it still goes to /post (though if I type in /archive as a url, it works -- not ideal, though). Confused. Could this have to do with my having installed friendly_id?
resources :posts, except: [:index]
get 'archive' => 'posts#index', as: :posts
You need to use something like match '/archive', :to => 'posts#index', :as => 'archived'. Then you will have a new route to the tune of archived_posts_path. The method posts_path does not dynamically changed based on custom matchers. You can always run rake routes to see a list of routes for your site.

How can I guess the route helpers that are created with match in Rails?

I have created a route in the routes.rb file like this:
match ':controller/:action/:id'
I tried invoking add_posts_path() and add_post_path() from my view and in both cases I got similar error messages like this one:
undefined method `add_post_path' for ...
I have tried declaring my match route both before and after the resources :posts declaration.
Are any route helpers created for such a route? I am unsure what helper methods can be used with such a match rule.
You can name routes with :as parameter
match '/foo/bar', to: 'foo#bar', as: 'foo_bar'
and then use foo_bar_path in your view
http://guides.rubyonrails.org/routing.html#naming-routes
If you have resources :posts, you have a helper new_post_path to add new posts. Run rake routes to see your apps routes.
add_post_path does't follow Rails routes convention for resources and if you need it, must add a custom method:
resources :posts do
get :add, :on => :collection
end
You can read more about this in this Rails guide.
When you define match ':controller/:action/:id', you set the format of your app's urls and their params, but this do not magically will define routes helpers.

RESTful route overriden by named route

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

Rails 3 route appends _index to route name

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.

Resources