Question

I have two models, admins and users. I have set scoped views = true within devise.rb. I also have generated two different set of devise views. For some reason when I click on edit admin registration, it gives me an error

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

It highlights this:

<%= gravatar_for @user %>

and points to "app/views/devise/registrations/edit.html.erb"

In my "config/initializer/devise.rb" file it has:

config.scoped_views = true

In my "config/routes.rb" file I have

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

In my "app/controllers/aregistrations_controller.rb" I have

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

In my views I have a devise folder - for the user model and an admins folder for the admin model.

In my "app/views/devise/registrations/edit.html.erb" I have

<%= gravatar_for @user %>
<a href="http://gravatar.com/emails">change</a></br></br></br></br>

I have it set up to edit your profile and your gravtar image for users only.

In my "app/views/layouts/_header.html.erb" file I have

<% if admin_signed_in? %>
<li><%= link_to "Edit Account", edit_admin_registration_path %></li>
<% else %> 
.....
<% end %>

Why does Devise look for the user gravatar when I click on the edit admins path and have scoped views on?

-------------------- edit In my **"app/controllers/users_controller.rb" ** file

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
Était-ce utile?

La solution

I solved this with the help from another post Override Devise Registrations Controller Two Times? I found that I needed to create a directory within my "app/controllers" directory. This is how you use the scoped controller:

"app/controllers/admin/registrations_controller.rb"

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

I can leave my first controller and keep it for the users "app/controllers/registrations_controller.rb"

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

My "config/routes.rb" will look like:

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

Autres conseils

From the looks of it the method in the User has not been established properly. Have you tried restarting your server so that the config loads properly?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top