我正在尝试实施一项政策,将帖子的创建限制为管理员或企业。反过来,只有创建帖子或管理员的记录所有者(企业)可以编辑。但是我收到一条错误消息,说“未定义的方法用户”有人可以在这里帮忙吗?
架构.rb
create_table "users", force: true do |t|
t.string "name"
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.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
create_table "posts", force: true do |t|
t.string "name"
t.string "title"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
用户.rb
def admin?
role == 'admin'
end
def biz?
role == 'biz'
end
Post_controller
def edit
@post = Post.find(params[:id])
authorize @post
end
Post.show.html.erb
<% if policy(@post).edit? %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<% end %>
应用程序.策略
def create?
user.present? && ( user.admin? || user.biz?)
end
def new?
create?
end
def update?
user.present? && (record.user == user || user.admin?)
end
def edit?
update?
end