【发布时间】:2015-05-27 17:17:23
【问题描述】:
我按照本指南为用户模型创建了授权: authentication-with-devise-and-cancancan
但在用户模型中使用管理员布尔列而不是角色表。以这种方式更改管理方法(用户模型)
def assign_role
self.admin = false if self.admin.nil?
end
def admin?
self.admin == true
end
一切正常。现在我创建了一个客户模型,并再次遵循相同的教程。当我尝试注册时,我收到未定义的方法“名称”,用于 current_customer 错误。有什么想法吗?
编辑:这是命令行中的代码:
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
所以我有客户模型:
class Customer < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
在架构中:
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
与用户模型的唯一区别是:
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
在用户中添加了一个布尔字段“admin”。使用 Devise 验证两种不同的模型是否存在冲突? 错误出现在第 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>
【问题讨论】:
-
您需要提供更多代码 - 您的客户模型是什么样的?它有
name属性吗? -
编辑了我的帖子...感谢您的帮助!!
标签: ruby-on-rails devise