Skip to content

Commit

Permalink
fixed the deletion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrobert committed Jun 23, 2020
1 parent bcbad93 commit e045b4f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 15 deletions.
5 changes: 1 addition & 4 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ def destroy
@group = current_user.groups.find(params[:id])
@group.destroy

respond_to do |format|
format.html { redirect_to groups_url, notice: 'group was successfully destroyed.' }
format.json { head :no_content }
end
redirect_back(fallback_location: root_path)
end

private
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@ class StaticPagesController < ApplicationController

def home
end

def external
user = User.find_by(id: current_user.id)
@external_user_transactions = user.transactions.where(group_id: nil).order(created_at: :desc)
@external_total = @external_user_transactions.sum(:amount)
end

def friends
@users = User.where.not(id: current_user.id)
@expenses = Expense.where.not(user_id: current_user.id)
end
end
18 changes: 10 additions & 8 deletions app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ class TransactionsController < ApplicationController
before_action :authenticate_user!

def index
@transaction = current_user.transactions.all.sort_by(&:created_at).reverse
@total = current_user.transactions.sum(:amount)
@transaction = current_user.transactions.all
@other_transaction = @transaction.where.not(group_id: nil)
@other_transaction_sorted = @other_transaction.sort_by(&:created_at).reverse

@total = @other_transaction.sum(:amount)

end

def new
@transaction = Transaction.new
end

def create

@transaction = current_user.transactions.new(transaction_params)

if @transaction.save

if @transaction.group_id.nil?

redirect_to home_external_path, notice: 'Transaction expense was successfully created.'
redirect_to external_path, notice: 'Transaction expense was successfully created.'

else

Expand Down Expand Up @@ -56,11 +61,8 @@ def destroy

@transaction = current_user.transactions.find(params[:id])
@transaction.destroy

respond_to do |format|
format.html { redirect_to transactions_url, notice: 'Transaction was successfully destroyed.' }
format.json { head :no_content }
end
redirect_back(fallback_location: root_path)


end

Expand Down
67 changes: 67 additions & 0 deletions app/views/static_pages/external.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<section class="transaction_header">
<div class="container t-d">
<div class="button">
<%= link_to root_path do %>
<i class="fa fa-bars" aria-hidden="true"></i>
<% end %>
</div>
<div class="title">
Extra transactions
</div>

<div class="search">
<i class="fa fa-search" aria-hidden="true"></i>
</div>
</div>

<h3 class ="text-center">most recent</h3>

</section>

<section class="transaction_body">
<div class="total_amount_sec">
<div class="amount_title">
total amount
</div>

<div class="actual_total_amount">
$ <%= @external_total %>
</div>
</div>
<div class="container t-c">

<% @external_user_transactions.each do |t| %>

<div class="card mb-3">
<div class="row no-gutters">
<div class="col-md-4 grp-icn">

<i class="fa fa-file-o" aria-hidden="true"></i>

</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title trns"> <%= t.name %> </h5>
<p class="card-text style-up"> <span>$</span> <%= t.amount %></p>
<p class="card-text"><small class="text-muted"><%= t.created_at %></small></p>

<%= link_to "edit", edit_transaction_path(t), :class => "btn btn-primary edit_btn" %>

<%= link_to "delete", transaction_path(t), :class => "btn btn-danger delete_grp_btn", data: {confirm: "Are you sure?"}, :method => :delete %>
</div>
</div>
</div>
</div>
<% end %>
</div>

<br>


<br>
<%= link_to "Add new", new_transaction_path , :class => "btn btn-success new_trans" %>
</section>




2 changes: 1 addition & 1 deletion app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</li>
<hr>
<li>
<i class="fa fa-map-marker" aria-hidden="true"></i> <%= link_to "All my External Transactions", "#", :class => "btn" %>
<i class="fa fa-map-marker" aria-hidden="true"></i> <%= link_to "All my External Transactions", external_path, :class => "btn" %>
</li>
<hr>
<li>
Expand Down
5 changes: 3 additions & 2 deletions app/views/transactions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ Transactions
</div>
<div class="container t-c">

<% @transaction.each do |t| %>
<% @other_transaction_sorted.each do |t| %>

<div class="card mb-3">
<div class="row no-gutters">
<div class="col-md-4 grp-icn">
<i class= "<%= t.group.icon %>" ></i>

<i class= "<%= t.group.icon %>" ></i>

</div>
<div class="col-md-8">
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
devise_for :users
resources :transactions
resources :groups

get '/external', to: 'static_pages#external'

# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

0 comments on commit e045b4f

Please sign in to comment.