0

我正在尝试为用户提供创建子域的能力。单击创建帐户时,系统会从“accounts/new”重定向到“accounts”,并且不会填充子域。

路线.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

account_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

新的.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 %>

日志中的错误

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"):
4

2 回答 2

0

这一行:

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

告诉Rails“只为一个帐户设置new和路由”。create

它基本上说“不要设置索引路由”

如果您想要一个索引路由(即显示所有帐户的列表),那么您需要将该行更新为例如:

resources :accounts, only: [:new, :create, :index]
于 2017-02-09T03:19:06.800 回答
0

:index路由添加到行: 资源 :accounts,仅:[:new, :create] 在 AccountsController创建索引操作并处理 渲染 json:YourObject

于 2017-02-09T06:33:35.020 回答