【问题标题】:No route matches [GET] "/accounts.json"没有路由匹配 [GET] "/accounts.json"
【发布时间】:2017-02-09 03:11:16
【问题描述】:

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

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

日志中的错误

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

【问题讨论】:

    标签: ruby-on-rails json ruby subdomain apartment-gem


    【解决方案1】:

    这一行:

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

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

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-21
        • 2014-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多