用多种模型设计视图

我有两个模型,管理员和用户。 我在devise.rb中设置了scoped views = true。 我还生成了两组不同的设计视图。 出于某种原因,当我点击编辑管理员注册时,它给了我一个错误

NoMethodError in Aregistrations#edit undefined method `email' for nil:NilClass 

它强调了这一点:

  

并指向“app / views / devise / registrations / edit.html.erb”

在我的“config / initializer / devise.rb”文件中,它具有:

 config.scoped_views = true 

在我的“config / routes.rb”文件中

 devise_for :admins, :controllers => {:registrations => "aregistrations"} devise_for :users, :controllers => {:registrations => "registrations"}, :path_prefix => 'd' resources :users, :only =>[:show] 

在我的“app / controllers / aregistrations_controller.rb”中,我有

 def update new_params = params.require(:admin).permit(:email, :username, :current_password, :password, :password_confirmation) change_password = true if params[:admin][:password].blank? params[:admin].delete("password") params[:admin].delete("password_confirmation") new_params = params.require(:admin).permit(:email, :username) change_password = false end @admin = Admin.find(current_admin.id) is_valid = false if change_password is_valid = @admin.update_with_password(new_params) else is_valid = @admin.update_without_password(new_params) end if is_valid set_flash_message :notice, :updated sign_in @admin, :bypass => true redirect_to after_update_path_for(@admin) else render "edit" end end 

在我的视图中,我有一个设计文件夹 – 用于用户模型和管理员模型的管理员文件夹。

在我的“app / views / devise / registrations / edit.html.erb”中,我有

  change



我已将其设置为仅为用户编辑您的个人资料和您的gravtar图像。

在我的“app / views / layouts / _header.html.erb”文件中

  
  • .....

    为什么Devise在点击编辑管理员路径并查看范围视图时会查找用户gravatar?

    ——————–编辑在我的**“app / controllers / users_controller.rb”**文件中

     class UsersController  [:show]  def index    @users = User.paginate(page: params[:page])  end  def show    @user = User.find_by_username(params[:id])    @reviews = @user.reviews.paginate(page: params[:page])  end end 

    我在另一篇文章Override Devise Registrations Controller两次的帮助下解决了这个问题? 我发现我需要在“app / controllers”目录中创建一个目录。 这是您使用范围控制器的方式:

    “应用程序/控制器/管理/ registrations_controller.rb”

     class Admin::RegistrationsController < Devise::RegistrationsController #Your Code Here end 

    我可以离开我的第一个控制器并为用户“app / controllers / registrations_controller.rb”保留它

     class RegistrationsController < Devise::RegistrationsController #Your Code Here end 

    我的“config / routes.rb”看起来像:

     devise_for :admins, :controllers => {:registrations => "admins/registrations"} devise_for :users, :controllers => {:registrations => "registrations"} 

    从它的外观来看,用户中的方法尚未正确建立。 您是否尝试重新启动服务器以便正确加载配置?