ボタンは管理者アカウントには表示されません。ボタンは管理者には表示されません
移行コード:
class DeviseCreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
## Database authenticatable
t.string :fname, null: false, default: ""
t.string :lname, null: false, default: ""
t.boolean :admin, default: false
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
...
モデルコード:
def admin?
self.admin == true
end
これは、ビューのコードです:
<% if current_user && current_user.admin? %>
<%= link_to 'New category', new_category_path %>
<% end %>
私はこのようなレールのコンソールから管理者設定:
をa = User.find(1)
=> #<User id: 1, fname: "", lname: "", admin: false, email: "[email protected]", created_at: "2017-05-10 21:29:29", updated_at: "2017- 05-10 22:08:28">
a.admin = true
=> true
a
=> #<User id: 1, fname: "", lname: "", admin: true, email: "[email protected]", created_at: "2017-05-10 21:29:29", updated_at: "2017-05-10 22:08:28">
私は間違っていましたか?助けてください.....ありがとう!あなたのコンソールで
'admin?'メソッドは必要ありません。単に 'current_user.admin? 'を使用してください。 –