Creating a specific custom route in rails 3 - ruby-on-rails

I have several levels of nested routing.
resources :departments do
resources :tasks do
collection do
get "report" => "tasks#report"
end
end
This a piece of it.
What I am attempting to do is create a custom route for a report.html.erb file. However, this route creates the path /department/:id/tasks/report
I would like to create the path /department/:id/tasks/:id/report
Is this possible? I considered creating a new controller and model for report but this seems to be inefficient.

try with:
resources :departments do
resources :tasks do
member do
get "report" => "tasks#report"
end
end
end
or just:
resources :departments do
resources :tasks do
get "report" => "tasks#report", :on => :member
end
end

Related

Rails - How to add a route to some resources

Some routes are for CRUD operations, such as client, phone_numbers and products. I have 'unique-index' attributes for many of the models. For example, in Client, I could have "email" or "username", whereas in Products I could have "serialnumber", and in PhoneNumber the actual "number" could be unique-index.
So, I have methods in my ApplicationController that receives a JSON in a POST request, with the attribute name and attribute value. The server checks if the value exists and informs the user if the input is valid or if value exists.
So, for these models, I have to declare a route that points to the "unique" method, such as below
routes.rb
resources :clients
resources :phone_numbers
resources :products
post 'clients/unique' => 'clients#unique'
post 'phone_numbers/unique' => 'phone_numbers#unique'
post 'products/unique' => 'products#unique'
My question is: Can I "group" these routes, without prefix (unlike namespace and scope) to just add post 'unique' to them? a pseudo-code would be like this
pseudo-code
group alias: 'modelsWithUniqueAttrs'
resources :clients
resources :phone_numbers
resources :products
add_route 'unique'
end
Try this
resources :clients, :phone_numbers, :products do
collection do
post :unique
end
end
Or
unique_routing = Proc.new do
collection do
post :unique
end
end
resources :clients, &unique_routing
resources :phone_numbers, &unique_routing
resources :products, &unique_routing
Or
unique_routing = Proc.new do
collection do
post :unique
end
end
resources :clients, :phone_numbers, :products, &unique_routing
You can define your routes like this:
resources :clients do
member do
post :unique => 'clients#unique'
end
end
resources :phone_numbers do
member do
post :unique => 'phone_numbers#unique'
end
end
resources :products do
member do
post :unique => 'products#unique'
end
end
These will give you routes like:
unique_client POST /clients/:id/unique(.:format) clients#unique
unique_phone_numbers POST /phone_numbers/:id/unique(.:format) phone_numbers#unique
unique_products POST /products/:id/unique(.:format) products#unique
Update
You can define routes using collection block like this:
resources :clients, only: [:uniq] do
collection do
post :uniq
end
end
which you give you routes like:
uniq_clients POST /clients/uniq(.:format) clients#uniq
But, this is still not what you want exactly. Because, you have to have the similar collection blocks for products and phone_numbers as well.

Rails 3 create a route to nested independent page

I have 3 levels of nesting.
routes.rb looks like this
resources :clients do
resources :departments do
resources :tasks
end
end
I would like to create a custom path that looks like this
/clients/:client_id/departments/:department_id/tasks/data
I have tried adding the following
resources :clients do
resources :departments do
resources :tasks
member do
get "data"
end
end
end
This creates the route
/clients/:client_id/departments/:department_id/tasks/:task_id/data
How would I remove the :task_id part the path?
A member route acts on a member, that's why it requires an id. A collection acts on a collection and so doesn't require an id.
resources :clients do
resources :departments do
resources :tasks do
collection do
get "data"
end
end
end
end
You should use
resources :clients do
resources :departments do
resources :tasks
get "data", :on => :collection
end
end

Unnamespaced routes within namespaced routes

I have several namespaced routes in an app. Here's a sample
namespace "battles" do
resources :teams do
resources :comments, :module => "comments", :controller=>'comments'
end
end
My problem is that all my resources with comments route to the comments/comments controller, but because :teams is in the battles namespace, then the app tries to route to battles/comments/comments
Is there a way to specify that the nested comment resource should route to the comments/comments controller, not the battle/comments/comments controller.
Try
scope :module => "battles" do
resources :teams do
resources :comments, :module => "comments", :controller=>'comments'
end
end
I gave up and just created a Battle::Comments controller. It results in code duplication so it's not ideal.

Adding a Route to the base URL of plural Resources

In Ruby on Rails, is there a way to add another RESTful action to the base URL of a plural resource? I'm looking for something like this:
resources :groups do
resources :users do
put on: :base, to: 'users#update_all'
end
end
Which would generate the route: [PUT] groups/:group_id/users => users#update_all
I've already tried doing this:
resources :groups do
resources :users
put 'users', on: :member, to: 'users#update_all'
end
But that doesn't preserve the value of params[:group_id] in the controller.
resources :users do
collection do
put '' => 'users#update_all' ## PUT /users
end
end
UPDATE
It would be recommended to do this though:
resources :users do
collection do
put 'update_all' ## PUT /users/update_all
end
end
Both route to the update_all action of the users controller.
RESOURCES
http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

Exposing a Specific Action of a Nested Route

Using this as an example contexted:
Post has_many comments
Comment belongs_to post
I have a route that looks like:
resources :posts do
resources :comments
end
How do i create a route that exposes comments#index?
An example use case would be... I want to list ALL comments in the system on a page. Essentially using the comments resource as if it's not nested when a user hits /comments
thank you!
Try this.
resources :posts do
resources :comments, :except => :index
end
match 'comments' => 'comments#index', :as => :comments
That said, I usually look to avoid routes like this because I like a tidy RESTful routes file, but sometimes it can't be helped.
Second option:
resources :posts do
resources :comments, :except => :index
get :comments, :on => :collection
end
In the second option, you'd want to remove the index action from the comments controller and create a comments action in your posts controller.

Resources