Skip to content

Commit

Permalink
Added a delete button with confirmation pop up box. Dried the code fo…
Browse files Browse the repository at this point in the history
…r new and edit by using a partial view _form
  • Loading branch information
amb54 committed Mar 24, 2017
1 parent 0919496 commit 68a39d8
Show file tree
Hide file tree
Showing 10 changed files with 673 additions and 77 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ In this baseline, you'll create a new Rails application and get started with two
- create a controller action for the task `index` page which contains an array of hard-coded tasks
- create an ERB view to display the tasks from the controller action

<!--

## Wave 1
This wave is where we introduce ActiveRecord to create a model. We use the model to persist our data.

Expand All @@ -28,9 +28,9 @@ This wave is where we introduce ActiveRecord to create a model. We use the model
- create at least 2 `Task` model instances using the `rails console`
1. update the controller's `index` action you created to retrieve and show `all` Task objects from the database
- you may need to update the view as well to use the model fields rather than the hard-coded data
-->

<!--


## Wave 2
In this wave, we will expand the actions we support and introduce forms for user interactivity and persistence.

Expand All @@ -43,9 +43,9 @@ In this wave, we will expand the actions we support and introduce forms for user
- update the task list to have a link to add a new task
- this will give the user a new page with a **form** with the appropriate task fields
- the site should take the user back to the task list after the new task is added
-->

<!--


## Wave 3
In this wave we will extend the interactivity with users, allowing them to edit existing tasks.

Expand All @@ -57,9 +57,9 @@ In this wave we will extend the interactivity with users, allowing them to edit
1. **Optional**
- DRY up your code by reusing the view code from the `new` functionality
- Hint: Rendering _partials_ in Rails
-->

<!--


## Wave 4
In this wave, we will add the ability to delete tasks. We will also add the ability for a user to mark a task complete.

Expand All @@ -70,4 +70,3 @@ In this wave, we will add the ability to delete tasks. We will also add the abil
1. Mark a task complete
- Add a button to the list of tasks on the home page that, when clicked, will mark a task complete
- Update the database with the task's completed date
-->
11 changes: 11 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def update
redirect_to task_path(task)
end

def destroy
task = Task.find(params[:id])
task.destroy
# Todo show the list of tska//

redirect_to tasks_path
#Another way instead of re_direct
# @tasks = Tasks.all
# render :index
end

private
def task_params
params.require(:task).permit(:name, :description)
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions app/views/tasks/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<section id = "new_task_section">
<%= form_for @task do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit %>
<% end %>
</section>
22 changes: 1 addition & 21 deletions app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@


<!-- <%= form_for @task do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit %>
<% end %> -->


<section id = "edit_task_section">
<h2>Edit task</h2>
<%= form_for @task do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit %>
<% end %>
</section>
<%= render partial: "form"%>
6 changes: 6 additions & 0 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<%= link_to task[:name], task_path(task.id) %>
<%= ": "%>
<%= task[:description] %>
<!-- Some sort of link to edit -->
<!-- Some sort of a link to delete -->
<%= link_to "Delete", task_path(task),
data: {method: :delete, confirm: "Are you sure you want to delete the task #{task.name}"}%>
<!-- <%###################= button_to "Delete", task_path(task), method: :delete%> -->
<!-- If the method is not specified the default is GET -->
</li>
<% end %>
</ul>
Expand Down
48 changes: 1 addition & 47 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,47 +1 @@
<!--
<form action="/tasks" method="post" accept-charset="utf-8">
-->
<!-- SPECIAL INPUT TO ALLOW RAILS TO USE THIS FORM -->

<!--
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
<label for="name">Task</label>
<input type="text" name="task[name]" value="" id="name">
<label for="description">Description</label>
<input type="text" name="task[description]" value="" id="description">
<input type="submit" value="Submit">
</form>
-->

<!--
#in new.html.erb
<%= datetime_local_field(:task, :completion_date) %>
#instead of:
#<label for="completion_date">Due Date</label>
#<input type="datetime" name="task[completion_date]" value="" id="completion_date">
-->

<!-- <%= form_for @task do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit %>
<% end %> -->



<section id = "new_task_section">
<h2>Enter a new task</h2>
<%= form_for @task do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.submit %>
<% end %>
</section>
<%= render partial: "form"%>
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
get 'tasks/:id/edit', to: 'tasks#edit', as: 'edit_task'
patch 'tasks/:id', to: 'tasks#update'


#Some sort of a route to delete a book
delete 'tasks/:id', to: 'tasks#destroy'


end
Binary file modified db/development.sqlite3
Binary file not shown.
Loading

0 comments on commit 68a39d8

Please sign in to comment.