1
votes

I'm new to RoR and Devise and i'm stuck in User Authentications with Devise. I'm developing a web site, which has admin pages in it. My structure looks like this:

app
 |-controllers
       |-admin
           |- user_list_controller.rb //Every user crud operations are in it.

 |-views
     |-admin
         |-user_list
             |-new.html.erb
             |-edit.html.erb
         |-devise //Also have devise views

UserListController:

class Admin::UserListController < ApplicationController
    layout 'admin/admin'
  def index
    @user_list = User.all
  end

  def new
    @user = User.new
  end

  def edit
  end

  def delete
  end

  def create_user

  end
end

What I want to do is that, I want to use Devise methods under this controller but this is where I stuck in. I created a UserController which base is Devise::RegistrationsController. But this time I got this error,

Could not find devise mapping for path "/admin/create_user". This may happen for two reasons: 1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]

It looks like a route.rb error and my route file is:

Rails.application.routes.draw do


  devise_for :users
  devise_scope :user do 
    # post "admin/add_user" =>"admin/user_list#create_user", as: :adduser
  end

   namespace :admin do
    root to: 'admin#index'
    get 'user_list', :to => 'user_list#index'
    post 'create_user', :to => "user#create"
    get 'new_user', :to => 'user_list#new'

    get 'user_proposals', :to => 'user_proposal_forms#index'
    get 'user_appointments', :to => 'user_appointments#index'
    get 'brands', :to => 'brands#index'
    get 'brand_makes', :to => 'brand_makes#index'
    get 'make_types', :to => 'make_types#index'
  end

end

And the result of rake routes is this:

Prefix Verb             URI Pattern                 Controller#Action new_user_session GET    /users/sign_in(.:format)    devise/sessions#new
    user_session POST   /users/sign_in(.:format)    devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
       user_password POST   /users/password(.:format) devise/passwords#create
new_user_password GET    /users/password/new(.:format)      devise/passwords#new
edit_user_password GET    /users/password/edit(.:format)  devise/passwords#edit
                  PATCH  /users/password(.:format)       devise/passwords#update
                  PUT    /users/password(.:format)       devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
       user_registration POST   /users(.:format)    devise/registrations#create
new_user_registration GET    /users/sign_up(.:format)devise/registrations#new edit_user_registration GET    /users/edit(.:format)  devise/registrations#edit
                      PATCH  /users(.:format)        devise/registrations#update
                         PUT    /users(.:format)     devise/registrations#update
                     DELETE /users(.:format)        devise/registrations#destroy
        admin_root GET    /admin(.:format)                   admin/admin#index
   admin_user_list GET    /admin/user_list(.:format) admin/user_list#index
admin_create_user POST   /admin/create_user(.:format)       admin/user#create
    admin_new_user GET    /admin/new_user(.:format)          admin/user_list#new
admin_user_proposalsGET /admin/user_proposals(.:format)    admin/user_proposal_forms#index
admin_user_appointments GET /admin/user_appointments(.:format) admin/user_appointments#index
admin_brands GET    /admin/brands(.:format)            admin/brands#index
admin_brand_makes GET  /admin/brand_makes(.:format)    admin/brand_makes#index
 admin_make_types GET    /admin/make_types(.:format)   admin/make_types#index

It looks messy, sorry for that. Finally my form_for looks like this:

<%= simple_form_for @user, url: admin_create_user_path, class: "form-horizontal" do |f| %>

     <%= render(:partial => "form", :locals => {:f => f}) %>

 <% end %>

So where did I make a mistake? I've read all the documents in Devise, tried so many things but couldn't solve the problem.

1

1 Answers

1
votes

In routes file use

 devise_for :users, :controllers => { registrations: 'registrations' }