nested routes

This commit is contained in:
CroneKorkN 2026-01-18 19:06:21 +01:00
parent 154d2c48e8
commit 6909497996
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw
6 changed files with 30 additions and 43 deletions

View file

@ -10,18 +10,12 @@ class ServersController < ApplicationController
end
def new
@server_templates = current_user.server_templates.order(:name)
if params[:server_template_id].present?
@server_template = current_user.server_templates.find(params[:server_template_id])
@server = current_user.servers.build(server_template: @server_template)
else
@server = current_user.servers.build
end
end
def create
template_id = params[:server_template_id] || server_params[:server_template_id]
@server_template = current_user.server_templates.find(template_id)
@server_template = current_user.server_templates.find(params[:server_template_id])
@server = current_user.servers.build(server_params)
@server.server_template = @server_template
@server.status = :stopped
@ -31,7 +25,6 @@ class ServersController < ApplicationController
SpawnServerJob.perform_later(@server.id)
redirect_to @server, notice: "Server spawning..."
else
@server_templates = current_user.server_templates.order(:name)
render :new, status: :unprocessable_entity
end
end

View file

@ -16,7 +16,7 @@
td = template.overlays.count
td
= link_to "Edit", edit_server_template_path(template), class: "btn btn--small"
= link_to "Spawn", new_server_path(server_template_id: template.id), class: "btn btn--small btn--success"
= link_to "Spawn", new_server_template_server_path(template), class: "btn btn--small btn--success"
= link_to "Delete", server_template_path(template), method: :delete, data: { confirm: "Sure?" }, class: "btn btn--small btn--danger"
- else
p No templates yet.

View file

@ -2,7 +2,7 @@
h2 = @server_template.name
= link_to "Edit", edit_server_template_path(@server_template), class: "btn btn--small"
= link_to "Delete", server_template_path(@server_template), method: :delete, data: { confirm: "Sure?" }, class: "btn btn--small btn--danger"
= link_to "Spawn Server", new_server_path(server_template_id: @server_template.id), class: "btn btn--primary"
= link_to "Spawn Server", new_server_template_server_path(@server_template), class: "btn btn--primary"
section.overlays
h3 Overlays

View file

@ -1,6 +1,6 @@
.servers
h2 Servers
= link_to "New Server", new_server_path, class: "btn btn--primary"
p Select a template to spawn a new server: = link_to "View Templates", server_templates_path
- if @servers.any?
table
@ -29,4 +29,4 @@
= link_to "View", server, class: "btn btn--small"
= link_to "Delete", server_path(server), method: :delete, data: { confirm: "Sure?" }, class: "btn btn--small btn--danger"
- else
p No servers yet. = link_to "Create one!", new_server_path
p No servers yet. = link_to "Create a template first", server_templates_path

View file

@ -1,8 +1,7 @@
.server_form
h2 Spawn New Server
h2 Spawn New Server for #{@server_template.name}
= form_with model: @server, local: true do |f|
= hidden_field_tag :server_template_id, @server_template.id if @server_template
= form_with model: @server, url: server_template_servers_path(@server_template), method: :post, local: true do |f|
- if @server.errors.any?
.alert.alert--error
h4 = pluralize(@server.errors.count, "error")
@ -10,11 +9,6 @@
- @server.errors.full_messages.each do |msg|
li = msg
- unless @server_template
.form-group
= f.label :server_template_id, "Template"
= f.select :server_template_id, options_from_collection_for_select(@server_templates, :id, :name, @server.server_template_id), { prompt: "Select a template" }, class: "form-control", required: true
.form-group
= f.label :name
= f.text_field :name, placeholder: "e.g., server1"
@ -23,7 +17,6 @@
= f.label :port, "Port (optional, auto-assigned if blank)"
= f.number_field :port, placeholder: "Auto: 27016-27999", min: 27016, max: 27999
- if @server_template
.template-preview
h3 Template Configuration

View file

@ -13,13 +13,14 @@ Rails.application.routes.draw do
# Server resources
resources :server_templates do
resources :overlays, only: [ :create, :destroy ]
resources :servers, only: [ :new, :create ]
end
resources :overlays, only: [ :index, :new, :create, :destroy ]
resources :job_logs, only: [ :index, :show ]
resources :servers do
resources :servers, only: [ :index, :show, :destroy ] do
member do
post :spawn
post :start