From d882c1a5217249b3e5a9e32fdc7d2af343dca83d Mon Sep 17 00:00:00 2001 From: Jez Caudle Date: Wed, 21 Jun 2023 11:07:34 +0100 Subject: [PATCH] Something not right here with Kate. --- app/controllers/credentials_controller.rb | 5 +++++ app/models/credential.rb | 1 + app/models/virtual.rb | 1 + app/views/credentials/_form.html.erb | 2 +- app/views/credentials/new.html.erb | 2 +- app/views/domains/show.html.erb | 5 +---- 6 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/credentials_controller.rb b/app/controllers/credentials_controller.rb index bf16207..700b7ba 100644 --- a/app/controllers/credentials_controller.rb +++ b/app/controllers/credentials_controller.rb @@ -1,5 +1,6 @@ class CredentialsController < ApplicationController before_action :set_credential, only: %i[ show edit update destroy ] + before_action :set_domain, only: %i[ new ] # GET /credentials or /credentials.json def index @@ -63,6 +64,10 @@ class CredentialsController < ApplicationController @credential = Credential.find(params[:id]) end + def set_domain + @domain = Domain.find(params[:domain_id]) + end + # Only allow a list of trusted parameters through. def credential_params params.require(:credential).permit(:email, :password) diff --git a/app/models/credential.rb b/app/models/credential.rb index 1c914ea..dd4a896 100644 --- a/app/models/credential.rb +++ b/app/models/credential.rb @@ -2,5 +2,6 @@ class Credential < ApplicationRecord belongs_to :domain validates :email, presence: true validates :email, uniqueness: true + validates :domain_id, presence: true validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "email must be valid"} end diff --git a/app/models/virtual.rb b/app/models/virtual.rb index f72b887..f03a3a1 100644 --- a/app/models/virtual.rb +++ b/app/models/virtual.rb @@ -3,6 +3,7 @@ class Virtual < ApplicationRecord validate :domain_name_exists validates :email, presence: true validates :destination, presence: true + validates :domain_id, presence: true validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "email must be valid"} validates :destination, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "destination must be valid"} validates :destination, uniqueness: {scope: :email, message: "a redirection pair must be unique"} diff --git a/app/views/credentials/_form.html.erb b/app/views/credentials/_form.html.erb index 5d71a5a..c06258a 100644 --- a/app/views/credentials/_form.html.erb +++ b/app/views/credentials/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with(model: credential) do |form| %> +<%= form_with(model: [@domain, credential]) do |form| %> <% if credential.errors.any? %>

<%= pluralize(credential.errors.count, "error") %> prohibited this credential from being saved:

diff --git a/app/views/credentials/new.html.erb b/app/views/credentials/new.html.erb index ed24c5d..c9f0b5f 100644 --- a/app/views/credentials/new.html.erb +++ b/app/views/credentials/new.html.erb @@ -5,5 +5,5 @@
- <%= link_to "Back to credentials", credentials_path %> + <%= link_to "Back to the domain", domain_path(@domain) %>
diff --git a/app/views/domains/show.html.erb b/app/views/domains/show.html.erb index ab491de..028b12a 100644 --- a/app/views/domains/show.html.erb +++ b/app/views/domains/show.html.erb @@ -2,10 +2,7 @@

<%= @domain.domain %>

- <%= link_to "Edit this domain", edit_domain_path(@domain) %> | - <%= link_to "Back to domains", domains_path %> - - <%= button_to "Destroy this domain", @domain, method: :delete %> + <%= link_to "Edit this domain", edit_domain_path(@domain) %> | <%= link_to "Back to domains", domains_path %> | <%= button_to "Destroy this domain", @domain, method: :delete %>

Credentials (<%= @domain.credentials.count %>)