0

Building a Rails 5 App with Ruby 2.4.0

I have an User (Devise) model and an Account model

I am trying to create a user through the accounts model but am getting a very unclear log output and the account and user are not being created.

Log output:

Started POST "/accounts" for 127.0.0.1 at 2018-04-07 15:25:10 -0600
Processing by AccountsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"c7H8MN2pbANsOsJH5PtX93+fAyqoLrBgZ0DfH7wQ8fCsMT0tgbr7TqeBML2/DbW7b4rwEiSlVY9aEFgRsK5uNA==", "owner"=>{"f_name"=>"xxxxxx", "m_name"=>"xxxxxx", "l_name"=>"xxxxxx", "office_country_code"=>"1", "office_telephone_number"=>"4036155915", "country_code"=>"1", "phone_number"=>"4036155915", "email"=>"xxxxxx@xxxxxx.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "account"=>{"subdomain"=>"TestDomain"}, "commit"=>"Create Account"}
   (1.0ms)  BEGIN
  Account Exists (1.7ms)  SELECT  1 AS one FROM "accounts" WHERE LOWER("accounts"."subdomain") = LOWER($1) LIMIT $2  [["subdomain", "testdomain"], ["LIMIT", 1]]
   (1.1ms)  ROLLBACK
Redirected to http://localhost:3000/accounts/new
Completed 302 Found in 119ms (ActiveRecord: 6.3ms)


Started GET "/accounts/new" for 127.0.0.1 at 2018-04-07 15:25:10 -0600
Processing by AccountsController#new as HTML
  Rendering accounts/new.html.erb within layouts/application
  Rendered accounts/new.html.erb within layouts/application (114.5ms)
Completed 200 OK in 1713ms (Views: 1669.1ms | ActiveRecord: 0.0ms)

my User model:

class User < ApplicationRecord

  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable

  validates :f_name, :l_name, :country_code, :phone_number, presence: true

end

My Account model:

class Account < ApplicationRecord

  after_initialize :set_default_status, :if => :new_record?
  before_validation :downcase_subdomain

  RESTRICTED_SUBDOMAINS = %w(www admin)

  enum account_status: [:active, :inactive, :suspended, :vip]

  belongs_to :owner, class_name: 'User'
  has_one :subscription

  validates :owner, presence: true
  validates :subdomain, presence: true,
            uniqueness: {case_sensitive: false },
            format: { with: /\A[\w\-]+\Z/i, message: 'contains invalid caracters'},
            exclusion: {in: RESTRICTED_SUBDOMAINS, message: 'this name is restricted'}

  accepts_nested_attributes_for :owner

  private

  def downcase_subdomain
    self.subdomain = subdomain.try(:downcase)
  end

  def set_default_status
    self.account_status ||= 1
  end

end

my Accounts controller:

class AccountsController < ApplicationController

  def new
    @account = Account.new
    @account.build_owner
  end

  def create
    @account = Account.new(account_params)
    if @account.save
      redirect_to root_path, notice: 'Welcome to RoadRouge - Account created successfully!'
    else
      redirect_to new_account_path, alert: 'A problem has occurred. Please try again.'
    end
  end

  private

  def account_params
    params.require(:account).permit(:subdomain, :owner_id, :subscription_id, :account_status, owner_attributes: [:f_name, :m_name, :l_name, :office_country_code, :office_telephone_number, :country_code, :phone_number, :email, :password, :password_confirmation])
  end

end

my Application controller

    class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception

      before_action :configure_permitted_parameters, if: :devise_controller?

      private

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:f_name, :m_name, :l_name, :office_country_code, :office_telephone_number, :country_code, :phone_number, :email, :password, :password_confirmation])
    devise_parameter_sanitizer.permit(:account_update, keys: [:f_name, :m_name, :l_name, :office_country_code, :office_telephone_number, :country_code, :phone_number, :email, :password, :password_confirmation])
  end
      end

    end

Im not sure if I'm overlooking something small here but I'm at an absolute loss. Any assistance would be greatly appreciated.

Shawn Wilson
  • 1,311
  • 14
  • 40
  • It says an account with the name 'testdomain' already exists. In your account model you have `:subdomain, uniqueness: {case_sensitive: false }` - meaning no one can create an account with the same subdomain name. – gwalshington Apr 07 '18 at 21:59
  • Besides the error message, the params you are receiving include the "owner" and the "account" at the same level (not an owner inside an account as you want based on `account_params`) – Pablo Apr 08 '18 at 11:55

2 Answers2

0

Given the queries, it looks like it stopped at the validation of the subdomain. is it possible the subdomain already exists? try surfacing @account.errors to see what is causing the issue

andrew21
  • 640
  • 5
  • 8
0

For the first. :subdomain, uniqueness: {case_sensitive: false } makes attribute subdomain unique for account table. Looks like testdomain may already exist.

Moreover, you really need to add id for owner_attributes:

def account_params
  params.require(:account).permit(
    :subdomain,
    :subscription_id,
    :account_status,
    owner_attributes: %i[id f_name m_name l_name office_country_code office_telephone_number country_code phone_number email password password_confirmation]
end
Michael Arkhipov
  • 737
  • 8
  • 19