0

I am attempting to provide a user with the ability to create a subdomain. When clicking Create Account, the system redirects from "accounts/new" to "accounts" and no subdomain is getting populated.

routes.rb

class SubdomainPresent
  def self.matches?(request)
    request.subdomain.present?
  end
end

class SubdomainBlank
  def self.matches?(request)
    request.subdomain.blank?
  end
end

Saasapp::Application.routes.draw do

  constraints(SubdomainPresent) do
    root 'projects#index', as: :subdomain_root
    devise_for :users, controllers: { registrations: 'users/registrations' } 
    resources :users, only: :index
    resources :projects, except: [:index, :show, :destroy]
  end

  constraints(SubdomainBlank) do
    root 'visitors#new'
    resources :accounts, only: [:new, :create]
  end
end

accounts_controller.rb

class AccountsController < ApplicationController
  skip_before_action :authenticate_user!, only: [:new, :create]


  def new
    @account = Account.new
  end

  def create
    @account = Account.new(account_params)
    if @account.valid?
      Apartment::Database.create(@account.subdomain)
      Apartment::Database.switch(@account.subdomain)
      @account.save
      redirect_to new_user_session_url(subdomain: @account.subdomain)
    else
      render action: 'new'
    end
  end

new.html.erb

 <%= simple_form_for @account do |f| %>
    <%= f.input :subdomain do %>
      <div class="input-group">
        <%= f.input_field :subdomain, class: 'form-control' %>
        <span class="input-group-addon">.demo.dev</span>
      </div>
    <% end %>
    <%= f.button :submit, class: 'btn-primary' %>
  <% end %>

error in logs

Started GET "/accounts.json" for 127.0.0.1 at 2017-02-08 21:56:08 -0500

ActionController::RoutingError (No route matches [GET] "/accounts.json"):
nomad
  • 45
  • 9

2 Answers2

0

This line:

resources :accounts, only: [:new, :create]

is telling Rails "only set up the new and create route for an account".

It basically says "do not set up an index route"

if you want an index route (ie that will show a list of all the accounts) then you need to update that line to eg:

resources :accounts, only: [:new, :create, :index]
Taryn East
  • 27,486
  • 9
  • 86
  • 108
0

Add :index route to line: resources :accounts, only: [:new, :create] In AccountsController create index action and handle render json: YourObject