【问题标题】:Routing error with multiple models - devise rails多个模型的路由错误 - 设计导轨
【发布时间】:2016-08-05 19:13:39
【问题描述】:

实际上,我在我的应用程序中使用了两个模型,即用户和管理员,并且我在使用 devise gem 时遵循了要处理的每个步骤。

而且我想要多次注册。当用户注册时,必须重定向到用户注册页面,当管理员注册时,必须重定向到管理员注册页面才能进行注册。

当我在用户下执行注册链接时,它给了我如下路由错误;

Routing Error
uninitialized constant Users::RegistrationsController

当我在 Admin 下执行 sign_up 链接时,它给了我如下路由错误;

Routing Error
uninitialized constant Admins::RegistrationsController

routes.rb

Rails.application.routes.draw do

    root "home#index"

    devise_for :users, controllers: {
        sessions: 'users/sessions',
        registrations: 'users/registrations'
    }   

    get '/aslani' => 'aslani#index', as: :authenticated_user_root

    devise_for :admins, controllers: {
        sessions: 'admins/sessions',
        registrations: 'admins/registrations'
    }


    get '/kola' => 'kola#index', as: :authenticated_admin_root
end

aslani/index.html.erb

<% if user_signed_in? %>
      I am Aslani.
       <%= link_to 'Log out', destroy_user_session_path, method: :delete %>
    <% else %>
      <%= link_to 'Log In', new_user_session_path %>
      <%= link_to 'Sign Up', new_user_registration_path %>
<% end %>

kola/index.html.erb

<% if admin_signed_in? %>
       I am Kola.
      <%= link_to 'Log out', destroy_admin_session_path, method: :delete %>
    <% else %>
      <%= link_to 'Log In', new_admin_session_path %></li>
      <%= link_to 'Sign Up', new_admin_registration_path %>
<% end %>

app/controllers/users/sessions_controller.rb

class Users::SessionsController < Devise::SessionsController
    def new
        super
    end

    def create 
        self.resource = warden.authenticate!(auth_options) 
        sign_in(resource_name, resource) 
        yield resource if block_given? 
        respond_with resource, location: after_sign_in_path_for(resource) 
    end

end

app/controllers/admins/sessions_controller.rb

class Admins::SessionsController < Devise::SessionsController
    def new
        super
    end

    def create 
        self.resource = warden.authenticate!(auth_options) 
        sign_in(resource_name, resource) 
        yield resource if block_given? 
        respond_with resource, location: after_sign_in_path_for(resource) 
    end

end

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create 
        super
    end

  def update
    super
  end
end 

app/controllers/admins/registrations_controller.rb

class Admins::RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create 
         super
    end

  def update
    super
  end
end 

欢迎提出任何建议。

提前谢谢你。

【问题讨论】:

  • 你是否也覆盖了registrations_controller?
  • 感谢您的回复。是的,我确实为用户和管理员覆盖了registrations_controllers。
  • 路由错误问题已解决,但我收到 Missing template users/registrations/create 和 Missing template admins/registrations/create 错误。看看我更新了我的帖子。
  • 在创建动作中添加超级或删除它,因为你什么都没写。
  • 谢谢。我添加了超级。

标签: ruby-on-rails devise


【解决方案1】:

根据与@Muhammad 的讨论

他收到缺少模板管理员/注册/创建错误, 为了解决这个问题,要么添加超级要么删除创建动作,因为他什么都没有覆盖

class Users::RegistrationsController < Devise::RegistrationsController
  def new
    super
  end  

  def create
    super
  end

  def update
    super
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多