I am trying to implement a policy that restricts the creation of a post to either admin or a business. In turn only the record owner(business) who created the post or admin can edit. But i get an error saying 'undefined method user' can anyone help out here?
Schema.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
User.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 %>
Application.policy
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