Nested resources with devise - ruby-on-rails

This is my routes file.
devise_for :users, :path => 'accounts'
resources :users do
resources :articles
end
How can i show all articles of
the users on the main page?

Try this (HAML):
- current_user.articles.each do |article|
= article.name
You have to be sure that you have built an association between users and articles correctly in your articles_controller.

Related

How to embed a route resource in a Devise route in Rails 6

Under normal circumstances if one wants to embed resources within other resources in Ruby on Rails in the routes.rb file it would look like this:
# routes.rb
resources :parents do
resources :children
end
The above will allow for a url like http://localhost:3000/parents/1/children.
My question is how to achieve the same result with the default devise_for :parents that exists in my routes.rb file?
I tried:
# routes.rb
devise_for :parents do
resources :children
end
and it did not work properly.
Any help is greatly appreciated!
devise_for only creates the routes related to signing up and in, so you would still use
resources :parents do
resources :children
end
in your routes for nested resource paths. There is a detailed answer here: Nested Resource with Devise - Rails3
If you generate the Devise controllers and views, you also need to specify those in your routes like this
devise_for :parents, controllers: {
sessions: 'parents/sessions',
registrations: 'parents/registrations'
}

Uninitialized constant Admin::Admin

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

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.

How do I nest devise users with other models?

I just made a new app, and am wondering how I would route it. A user has_many companies,but how how do I route it so? I am using devise.
::Application.routes.draw do
devise_for :users do
resources :companies
end
root :to => "home#index"
end
I'd suggest separating between devise routes and other app routes:
devise_for :users, :path => 'accounts'
resources :users do
resources :companies
end
This also means that devise will use /accounts/* instead of /users/* for its authentication paths and so /users/* will remain free for your use.
You can also look at devise's routing documentation.

Route conversion for map.resources in rails 3

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

Resources