85 lines
2.6 KiB
Ruby
85 lines
2.6 KiB
Ruby
class ProjectsController < ApplicationController
|
|
before_action :set_project, only: %i[ show edit update destroy ]
|
|
|
|
# GET /projects or /projects.json
|
|
def index
|
|
@projects = Project.all
|
|
end
|
|
|
|
# GET /projects/1 or /projects/1.json
|
|
def show
|
|
end
|
|
|
|
# GET /projects/new
|
|
def new
|
|
@project = Project.new
|
|
@project.subprojects.build # initialize one subproject
|
|
@project.subprojects.each do |subproject|
|
|
subproject.build_client
|
|
subproject.build_owner
|
|
subproject.build_builder
|
|
end
|
|
end
|
|
|
|
# GET /projects/1/edit
|
|
def edit
|
|
end
|
|
|
|
# POST /projects or /projects.json
|
|
def create
|
|
@project = Project.new(project_params)
|
|
|
|
respond_to do |format|
|
|
if @project.save
|
|
format.html { redirect_to @project, notice: "Project was successfully created." }
|
|
format.json { render :show, status: :created, location: @project }
|
|
else
|
|
format.html { render :new, status: :unprocessable_entity }
|
|
format.json { render json: @project.errors, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
# PATCH/PUT /projects/1 or /projects/1.json
|
|
def update
|
|
respond_to do |format|
|
|
if @project.update(project_params)
|
|
format.html { redirect_to @project, notice: "Project was successfully updated." }
|
|
format.json { render :show, status: :ok, location: @project }
|
|
else
|
|
format.html { render :edit, status: :unprocessable_entity }
|
|
format.json { render json: @project.errors, status: :unprocessable_entity }
|
|
end
|
|
end
|
|
end
|
|
|
|
# DELETE /projects/1 or /projects/1.json
|
|
def destroy
|
|
@project.destroy!
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to projects_path, status: :see_other, notice: "Project was successfully destroyed." }
|
|
format.json { head :no_content }
|
|
end
|
|
end
|
|
|
|
private
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
def set_project
|
|
@project = Project.find(params.expect(:id))
|
|
end
|
|
|
|
# Only allow a list of trusted parameters through.
|
|
def project_params
|
|
params.require(:project).permit(
|
|
:name,
|
|
subprojects_attributes: [
|
|
:id, :subproject_name, :client_id, :owner_id, :builder_id, :_destroy,
|
|
client_attributes: [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone],
|
|
owner_attributes: [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone],
|
|
builder_attributes: [:company_name, :firstname, :lastname, :streetname, :zipcode, :city, :country, :email, :phone]
|
|
]
|
|
)
|
|
end
|
|
end
|