Skip to content

Commit ab7dbb0

Browse files
committed
Add Project actions
1 parent 62cba95 commit ab7dbb0

8 files changed

Lines changed: 173 additions & 9 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
22
protect_from_forgery with: :exception
33

44
before_action :configure_permitted_parameters, if: :devise_controller?
5+
before_action :authenticate_user!
56

67
protected
78

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class ProjectsController < ApplicationController
2+
before_action :set_project, only: [:show, :edit, :update, :destroy]
3+
4+
def index
5+
@projects = Project.all.order(created_at: :desc)
6+
end
7+
8+
def show
9+
@task = Task.new
10+
end
11+
12+
def new
13+
@project = Project.new
14+
end
15+
16+
def edit
17+
end
18+
19+
def create
20+
@project = current_user.projects.new(project_params)
21+
22+
if @project.save
23+
redirect_to @project, notice: 'Project was successfully created.'
24+
else
25+
render :new
26+
end
27+
end
28+
29+
def update
30+
if @project.update(project_params)
31+
redirect_to @project, notice: 'Project was successfully updated.'
32+
else
33+
render :edit
34+
end
35+
end
36+
37+
def destroy
38+
@project.destroy
39+
redirect_to projects_url, notice: 'Project was successfully destroyed.'
40+
end
41+
42+
private
43+
44+
def set_project
45+
@project = current_user.projects.find(params[:id])
46+
end
47+
48+
def project_params
49+
params.require(:project).permit(:name, :description)
50+
end
51+
end

app/views/projects/_form.html.erb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<%= form_with(model: project, local: true) do |form| %>
2+
<% if project.errors.any? %>
3+
<ul>
4+
<% project.errors.full_messages.each do |message| %>
5+
<li><%= message %></li>
6+
<% end %>
7+
</ul>
8+
<% end %>
9+
10+
<div class="flex flex-col mt-4">
11+
<%= form.label :name %>
12+
<%= form.text_field :name, class: 'flex-grow input' %>
13+
</div>
14+
15+
<div class="flex flex-col mt-4">
16+
<%= form.label :description %>
17+
<%= form.text_area :description, class: 'flex-grow input h-36' %>
18+
</div>
19+
20+
<div class="flex flex-col mt-4">
21+
<% if project.persisted? %>
22+
<div class="float-right">
23+
<%= link_to 'Destroy', project, method: :delete, class: "text-red-500", data: { confirm: 'Are you sure?' } %>
24+
</div>
25+
<% end %>
26+
27+
<div class="flex flex-col">
28+
<%= form.submit class: 'btn btn-primary' %>
29+
</div>
30+
</div>
31+
<% end %>

app/views/projects/edit.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="lg:px-10 px-4 py-5">
2+
<div class="flex flex-col items-center flex-1 h-full justify-center px-4 sm:px-0">
3+
<h1 class="mb-5 text-xl">Edit project</h1>
4+
<div class="flex rounded-lg shadow-lg w-full sm:w-3/4 lg:w-1/2 bg-white sm:mx-0">
5+
<div class="flex flex-col w-full p-4">
6+
<div class="flex flex-col flex-1 justify-center mb-8">
7+
<%= render 'form', project: @project %>
8+
</div>
9+
</div>
10+
</div>
11+
</div>
12+
</div>

app/views/projects/index.html.erb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<div class="container mx-auto my-8 px-4">
2+
<div class="flex justify-between items-center mb-4">
3+
<h1 class="text-3xl">Projects</h1>
4+
<%= link_to 'New project', new_project_path, class: "btn btn-primary" %>
5+
</div>
6+
7+
<% if @projects.any? %>
8+
<div class="bg-white rounded shadow">
9+
<table class="w-full">
10+
<tbody>
11+
<% @projects.each do |project| %>
12+
<tr class="group border-t border-gray-400 hover:bg-gray-100">
13+
<td class="p-3"><%= project.name %></td>
14+
<td>
15+
<%= link_to "View", project, class: "btn btn-primary" %>
16+
<%= link_to "Edit", edit_project_path(project), class: "btn btn-success" %>
17+
</td>
18+
</tr>
19+
<% end %>
20+
</tbody>
21+
</table>
22+
</div>
23+
<% else %>
24+
<div class="bg-white rounded shadow flex items-center justify-between p-8">
25+
<div class="flex-1 text-center">
26+
<p class="text-2xl font-semibold">Create your first project</p>
27+
</div>
28+
</div>
29+
<% end %>
30+
</div>

app/views/projects/new.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="lg:px-10 px-4 py-5">
2+
<div class="flex flex-col items-center flex-1 h-full justify-center px-4 sm:px-0">
3+
<h1 class="mb-5 text-xl">New project</h1>
4+
<div class="flex rounded-lg shadow-lg w-full sm:w-3/4 lg:w-1/2 bg-white sm:mx-0">
5+
<div class="flex flex-col w-full p-4">
6+
<div class="flex flex-col flex-1 justify-center mb-8">
7+
<%= render 'form', project: @project %>
8+
</div>
9+
</div>
10+
</div>
11+
</div>
12+
</div>

app/views/projects/show.html.erb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<div class="flex flex-col items-center justify-center">
2+
<div class="flex items-center justify-between mb-5">
3+
<h1 class="text-xl mr-5"><%= @project.name %></h1>
4+
<div class="flex items-center">
5+
<%= link_to 'All Projects', authenticated_root_path, class: 'text-blue-500 mr-5' %>
6+
<%= link_to 'Edit', edit_project_path(@project), class: 'text-blue-500' %>
7+
</div>
8+
</div>
9+
<div class="flex rounded-lg shadow-lg w-full sm:w-3/4 lg:w-1/2 bg-white sm:mx-0">
10+
<div class="flex flex-col w-full p-4">
11+
<div class="flex flex-col flex-1 justify-center">
12+
<h2 class="text-lg mb-3">Tasks</h2>
13+
<%= form_with(model: [@project, @task]) do |form| %>
14+
<div class="flex mb-3">
15+
<%= form.text_field :content, class: 'w-full rounded-lg mr-2', placeholder: 'Add a task' %>
16+
<%= form.submit 'Create', class: 'btn btn-primary' %>
17+
</div>
18+
<div class="mb-3">
19+
<div data-target="forms.errorsList"></div>
20+
</div>
21+
<% end %>
22+
</div>
23+
</div>
24+
</div>
25+
</div>

config/routes.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
Rails.application.routes.draw do
2-
unauthenticated :user do
3-
devise_scope :user do
4-
root to: 'unauthenticated#index', as: :unauthenticated_root
5-
end
6-
end
7-
8-
authenticated :user do
9-
root to: 'home#index', as: :authenticated_root
2+
unauthenticated :user do
3+
devise_scope :user do
4+
root to: 'unauthenticated#index', as: :unauthenticated_root
105
end
6+
end
117

8+
authenticated :user do
9+
root to: 'projects#index', as: :authenticated_root
10+
end
1211

1312
devise_for :users
14-
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
13+
14+
resources :projects do
15+
resources :tasks, only: :create
16+
end
1517
end

0 commit comments

Comments
 (0)