Skip to content

Commit

Permalink
Merge pull request #35 from RileyManda/food-component-authorization
Browse files Browse the repository at this point in the history
Food component authorization
  • Loading branch information
yin-ka authored Oct 19, 2023
2 parents 9b5ee3d + 0da6881 commit b2002a7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/controllers/food_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class FoodController < ApplicationController
load_and_authorize_resource
def new
@food = Food.new
end
Expand Down Expand Up @@ -27,7 +28,7 @@ def destroy
@food = Food.find(params[:id])
@food.destroy

redirect_to food_path, notice: 'Food successfully deleted.'
redirect_to food_index_path, notice: 'Food successfully deleted.'
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Ability

def initialize(user)
user ||= User.new
can :manage, Food, user_id: user.id
can :manage, Recipe, user_id: user.id
# Define abilities for the user here. For example:
#
Expand Down
6 changes: 4 additions & 2 deletions app/views/food/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
<tr>
<td><%= food.name %></td>
<td><%= food.measurement_unit %></td>
<td><%= food.price %></td>
<td><%= food.quantity %></td>
<td>&#x20A6; <%= food.price %></td>
<% if can? :manage, food %>
<td> <%= button_to "Delete", food_path(food), method: :delete, data: { turbo_method: :delete, confirm: "Are you sure?", turbo_confirm: "Are you sure?" }, class: 'btn btn-link' %><td>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
root 'food#index'
devise_for :users

resources :food, only: [:index, :new, :create] do
resources :food, only: [:index, :show, :new, :create, :destroy] do
resources :recipe, controller: 'recipe' do
post 'add_ingredient_to_recipe', on: :member
end
Expand Down

0 comments on commit b2002a7

Please sign in to comment.