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.
Related
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'
}
ruby 2.3.3,
rails 5.1.6.1
I am trying to edit a user, after i click on the update, my app loads to this:
No route matches [PUT] "/users/edit.user"
# routes.rb
Rails.application.routes.draw do
devise_for :users
resources :posts
resources :projects
resources :contacts, only: [:new, :create]
get 'welcome/index'
root 'welcome#index'
get '*path' => redirect('/')
end
seems to be a problem with your routes.rb file
check this guide -> https://guides.rubyonrails.org/v5.0/routing.html#resources-on-the-web
from what I can tell you are not defining the routes pointing to your UsersController (devise_for :users doesn't do that, it defines completely other set of routes)
your routes.rb should look roughly like below:
Rails.application.routes.draw do
resources :users
...some other routes (like devise_for :users)
end
resources :users will define routes like /users for showing all user resource or /users/:id/edit for editing the user resource.
Now that you have those routes the will point to the UsersController class which should be defined in the users_controller.rb. In the controller you should define appropriate actions (like edit or update) that will update your user resource
I hope this is going to help solving your problem.
I have a user model which has admin and manager role column (using devise), need to have different sign in page for admins and manager
devise_for :users, controllers: {
sessions: 'users/sessions'
}
as :user do
namespace :admins do
get 'sign_in', to: 'sessions#new'
post 'sign_in', to: 'sessions#create'
end
end
for admin role it has access the Admins::SessionsController controller and for manager it has to access Users::SessionsController, how can I specify that in routes without using devise_for ?
You need different scopes for user and admin.
devise_for :users, skip: :all
devise_scope :user do
# custom routes here
end
devise_scope :admin do
# custom routes here
end
Note that the resource is plural for devise_for but singular for devise_scope.
I am trying to add followers to my rails application. I am seeing the following error when I run rails s to start my server:
Invalid route name, already in use: 'user' (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
Routes
Rails.application.routes.draw do
devise_for :users
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
resources :posts do
member do
post '/like' => 'posts#like'
end
end
get ':username' => 'users#show', as: 'user'
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
It may be helpful to know that I added:
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
Before I added the above, my rails app worked.
I'm sure it's probably a simple fix, but I am new to rails. I've been tinkering with routes and searching online for over an hour, trying to understand and solve the problem. I'm hoping someone more knowledgeable can guide me in the right direction.
Since you are using devise there are some routes already created by devise_for users so avoid using such routes which has conflict with already defined routes. Instead of using as: :user put another relevant name like as::user_profile
Hope it helps
In your case, Devise routes and Users routes are conflicting. You need to change one of them. You can either put Devise under a specific prefix like,
devise_for :users, :path_prefix => 'auth'
resources :users
Reference: https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface
Please do not forget, that there is "as" option too.
devise_for :users, as: 'some_unique_prefix'
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.