【发布时间】:2014-01-21 16:25:09
【问题描述】:
我有两个模型,管理员和用户。我在 devise.rb 中设置了 scoped views = true。我还生成了两组不同的设计视图。出于某种原因,当我点击编辑管理员注册时,它给了我一个错误
NoMethodError in Aregistrations#edit
undefined method `email' for nil:NilClass
它强调了这一点:
<%= gravatar_for @user %>
并指向“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”中我有
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a></br></br></br></br>
我已将其设置为仅为用户编辑您的个人资料和您的重力图像。
在我的 “app/views/layouts/_header.html.erb” 文件中
<% if admin_signed_in? %>
<li><%= link_to "Edit Account", edit_admin_registration_path %></li>
<% else %>
.....
<% end %>
为什么当我单击编辑管理员路径并具有范围视图时,Devise 会查找用户 gravatar?
-------------------- 编辑 在我的**“app/controllers/users_controller.rb”**文件中
class UsersController < ApplicationController
before_filter :authenticate_admin!, :except => [: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
【问题讨论】:
标签: ruby-on-rails model-view-controller devise ruby-on-rails-4 gravatar