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.
Related
Using Ruby: 2.3.1p112 and Rails: 3.2.12
I'm trying call a demo method in my controller. So, in my _form.html.erb I have:
<%= link_to 'Demo', "/admin/clinics/"+#clinic.id.to_s+"/demo" %>
In my routes.rb:
match "/admin" => "admin#index", :as => :admin
namespace :admin do
resources :admin_users
resources :health_plan_tables
resources :health_aid_tables
resources :clients
resources :clinics
resources :specialties
resources :qualifications
resources :profissionals
resources :addresses
resources :documents
resources :banners
root :to => 'banners#index'
get 'logout' => 'devise/sessions#destroy'
get 'clinics/:id/demo', to: 'admin/clinics#demo', as: 'demo'
end
My clinics_controller.rb is inside the folder controllers/admin, and I just have:
def demo
print "hello"
end
So, when I click on link, error message appears Uninitialized constant Admin::Admin.
Any ideia how to fix it?
Since you are already defining your demo route inside a namespace there is no need to specify admin/clinics#demo, just clinics#demo will be necessary:
namespace :admin do
resources :admin_users
resources :health_plan_tables
resources :health_aid_tables
resources :clients
resources :clinics
resources :specialties
resources :qualifications
resources :profissionals
resources :addresses
resources :documents
resources :banners
root :to => 'banners#index'
get 'logout' => 'devise/sessions#destroy'
get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
end
According to the error log you're looking for a controller namespaced under admin/admin/clinics (it says that in the controller part of the params).
Change the bottom route to not include admin (it's already namespaced and you're effectively namespacing it twice):
get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
This will route to the correct controller, admin/clinics, instead of admin/admin/clinics
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
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
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
Currently, I have:
map.resources :users do |user|
user.resources :blogs
end
How do I turn this into a match or resources in rails 3+?
My attempt would be:
resources :users do
user.resources :blogs
end
I got most of my information on route changes from here.
resources :users do
resources :blogs
end
Since you do not have |user| in the Rails 3 block, you cannot refer to it. In addition, you shouldn't need to:
resources :users do
resources :blogs
end
Rails routing guide explains this. You should be able to use
resources :users do
resources :blogs
end