0
votes

I created authorization for user model following this guide: authentication-with-devise-and-cancancan

but with admin boolean column in user model instead the role table. Changing admin method (user model) in this way

def assign_role
 self.admin = false if self.admin.nil?
end

def admin?
 self.admin == true
end

Everything works fine. Now I created a customer model and I followed again same tutorial. When I try to sign up i receive undefined method `name' for current_customer error. Any ideas?

Edit: this is the code in command line:

rails g scaffold user name:string
rails g devise User
bundle exec rake db:migrate

rails g scaffold customer name:string
rails g devise Customer
bundle exec rake db:migrate

so I have in customer model:

class Customer < ActiveRecord::Base

devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
end

and in schema:

create_table "customers", force: :cascade do |t|
t.string   "name"
t.string   "surname"
t.string   "nickname"
t.datetime "created_at",                          null: false
t.datetime "updated_at",                          null: false
t.string   "email",                  default: "", null: false
t.string   "encrypted_password",     default: "", null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          default: 0,  null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet     "current_sign_in_ip"
t.inet     "last_sign_in_ip"
end

 add_index "customers", ["email"], name: "index_customers_on_email", unique: true, using: :btree
 add_index "customers", ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true, using: :btree

The only difference with User model is:

validates_presence_of :name
before_save :assign_role

def assign_role
 self.admin = false if self.admin.nil?
end

def admin?
 self.admin == true
end

and a boolean field "admin" added in user. There are conflict using Devise for authenticate two different models? The error is in the application layout in line 6:

<div class='container'>

<%= yield %>

<% if customer_signed_in? %>
Signed in as <%= current_customer.name %>. Not you?
<%= link_to "Edit profile", edit_customer_registration_path %>
<%= link_to "Sign out", destroy_customer_session_path, :method => :delete    %>
<% else %>
<%= link_to "Sign up", new_customer_registration_path %> or <%= link_to    "sign in", new_customer_session_path %>
<% end %>

<% flash.each do |name, msg| %>

<%= content_tag :div, msg, id: "flash_#{name}" %>
<% end %>
</div>
1
You'll need to provide more code - what does your customer model look like? Does it have a name attribute?Bart Jedrocha
Edited my post... Thanks for any help!!druido82

1 Answers

1
votes

Make sure that your model, Customer", has a property called "name". If not, run the following rake command:

$ rails g migration AddNameToCustomer name:string
$ rake db:migrate