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? %>