I have just installed Pundit on my app. I have managed to implement pundit for the new create and show methods however when it comes to my destroy and up vote methods I get the following error Pundit::NotDefinedError in HairstylesController#upvote unable to find policy `NilClassPolicy` for `nil
here is my repo for ease of understanding if needed: https://github.com/Angela-Inniss/hair-do
I have authorized the two methods, 'upvote' and 'destroy' in my controller. I have updated the relevant policy. Please see code below.
Controller with methods in question:
def destroy
authorize @hairstyle
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.destroy
redirect_to hairstyles_path
end
def upvote
authorize @hairstyle
@hairstyle = Hairstyle.find(params[:id])
@hairstyle.upvote_from current_user
redirect_to hairstyles_path
end
pundit policy.rb file:
def upvote?
record.user == user
# - record: the restaurant passed to the `authorize` method in controller
# - user: the `current_user` signed in with Devise.
end
def destroy?
record.user == user
end
index.html.erb file destroy html
<% if policy(hairstyle).destroy? %>
<p><%= link_to "Delete", hairstyle_path(hairstyle),method: :delete, data: { confirm: "Are you sure?" }%></p>
<% end %>
index.html.erb file upvote html (which worked before i added pundit )
<%= link_to like_hairstyle_path(hairstyle), method: :put do %>
<i class="fa fa-heart">
<span><%= hairstyle.get_upvotes.size %><span>
</i>
<% end %>
I would like the user to be able to upvote on a hairstyle I would like the user to be able to delete their hairstlye.