Finish routes for projects
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled

This commit is contained in:
2025-10-27 13:20:20 +01:00
parent 0dc4dd623d
commit 972d4c24ea
9 changed files with 73 additions and 2 deletions

View File

@@ -1,10 +1,48 @@
class ProjectsController < ApplicationController
before_action :set_project, only: %i[ show edit update destroy ]
def index
@projects = Project.all
end
def show
end
def new
@project = Project.new
end
def edit
end
def create
@project = Project.new(project_params)
if @project.save
redirect_to @project, notice: "Project successfully created."
else
render :new, status: :unprocessable_entity
end
end
def update
if @project.update(project_params)
redirect_to @project, notice: "Project successfully updated.", status: :see_other
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@project.destroy!
redirect_to projects_path, notice: "Project successfully deleted.", status: :see_other
end
private
def set_project
@project = Project.find(params[:id])
end
def project_params
params.expect(project: [ :name ])
end
end

View File

@@ -1,2 +1,3 @@
class Project < ApplicationRecord
validates :name, presence: true
end

View File

@@ -24,6 +24,9 @@
</head>
<body>
<% if flash[:notice] %>
<p class="flash"><%= flash[:notice] %></p>
<% end %>
<%= yield %>
</body>
</html>

View File

@@ -0,0 +1,21 @@
<%= form_with model: project do |f| %>
<% if project.errors.any? %>
<div class="errors">
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@@ -0,0 +1,3 @@
<h1>Edit Project</h1>
<%= render "form", project: @project %>

View File

@@ -3,3 +3,4 @@
<% @projects.each do |project| %>
<li><%= link_to project.name, project_path(project) %></li>
<% end %>
<p><%= link_to "New project", new_project_path %></p>

View File

@@ -0,0 +1,3 @@
<h1>New project</h1>
<%= render "form", project: @project %>

View File

@@ -1,2 +1,4 @@
<h1><%= @project.name %></h1>
<%= link_to "Edit", edit_project_path %>
<%= button_to "Delete", project_path, method: :delete %>
<%= link_to "Back", projects_path %>

View File

@@ -1,7 +1,6 @@
Rails.application.routes.draw do
resources :todos
get "/projects", to: "projects#index"
get "/projects/:id", to: "projects#show", as: "project"
resources :projects
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html