Finish routes for projects
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
class Project < ApplicationRecord
|
||||
validates :name, presence: true
|
||||
end
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<% if flash[:notice] %>
|
||||
<p class="flash"><%= flash[:notice] %></p>
|
||||
<% end %>
|
||||
<%= yield %>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
21
app/views/projects/_form.html.erb
Normal file
21
app/views/projects/_form.html.erb
Normal 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 %>
|
||||
3
app/views/projects/edit.html.erb
Normal file
3
app/views/projects/edit.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<h1>Edit Project</h1>
|
||||
|
||||
<%= render "form", project: @project %>
|
||||
@@ -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>
|
||||
3
app/views/projects/new.html.erb
Normal file
3
app/views/projects/new.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<h1>New project</h1>
|
||||
|
||||
<%= render "form", project: @project %>
|
||||
@@ -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 %>
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user