1

I am new to rails and I am building a blog like application called story teller.I have used devise to have a sign up, sign in model.I also tweaked and used the devise features allowing me to add a user-name during sign up and then sign in with either username or email.Next I added a devise admin model.However when I go to admin signup or signin page, it throws the following errors http://localhost:3000/admins/sign_in : NoMethodError in Devise::Sessions#new

http://localhost:3000/admins/sign_up : NoMethodError in Devise::Registrations#new

My devise session new form i.e new.html.erb in devise/session view is here

<h2>Log in</h2>

<%= form_for(resource, as: resource_name,
url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :login %><br />
<%= f.text_field :login, autofocus: true %>
</div>

<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>

<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>

<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>

<%= render "devise/shared/links" %>

My devise registration new form i.e new.html.erb in devise/registration is here

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name,
url:     registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, autofocus: true %>
</div>


<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>

<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>

<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>

<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>

<%= render "devise/shared/links" %>

This is my admin.rb ie admin model

class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end

I am very confused here and not able to find a work around.Please forgive me if the questions seems stupid and bear with me.Also let me know if I need to add some more information.

Mahesh Mesta
  • 793
  • 1
  • 11
  • 28
  • 1
    Please read Gjaldon answer [link](http://stackoverflow.com/questions/20865184/nomethoderror-in-deviseregistrationsnew) http://stackoverflow.com/questions/20865184/nomethoderror-in-deviseregistrationsnew If you still have a problem please post your routes for Devise from your routes.rb for inspection. – Wojciech Szymanski Jan 25 '16 at 10:40
  • Hi.It still doesnt works after refering to gjaldon answer.My routes.rb is Rails.application.routes.draw do devise_for :admins devise_for :users resources :profiles resources :categories resources :stories do resources :comments end get 'stories/index' root to: 'stories#index' end – Mahesh Mesta Jan 25 '16 at 11:14
  • 1
    It throws this error precisely for admin/sign_up undefined method `username' for # – Mahesh Mesta Jan 25 '16 at 11:16
  • Have you migrated your `Admin` model? Seems like you don't have a `username` field for it? – Richard Peck Jan 25 '16 at 11:32
  • You have to configure permitted parameters in your app configuration as per https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address – Wojciech Szymanski Jan 25 '16 at 11:35
  • 1
    OK.I got it..You were correct @Rich Peck.There was no user name field in my admin model, and no login method for the same as well.After adding them, they work perfectly fine.Thank you so much Wojciech Szymanski and Rich Peck – Mahesh Mesta Jan 25 '16 at 12:31
  • Pleasure. Would you like me to write an answer for posterity? – Richard Peck Jan 25 '16 at 12:32

0 Answers0