0

This is my error message :No route matches [GET] "/entreprises/10/create_tenant"

I m trying to permit to create a tenant (gem apartment) when you list your own entreprises.

In entreprise model is have an attribute called subdmain which should be used as tenant name.

thanks for any help.

My router is

    class SubdmainConstraint
  def self.matches?(request)
    subdmains = %w{ www admin public test }
    request.subdmain.present? && !subdmains.include?(request.subdmain)
  end
end

Rails.application.routes.draw do

  constraints SubdmainConstraint do

  end

  get 'layouts/confirmed'
  get 'pages/home'
  get 'entreprises/listing'

  namespace :admin do
    resources :users

      root to: "users#index"
    end
  devise_for :users, :controllers => { registrations: 'registrations', confirmations: 'confirmations'}
  resources :users
  resources :entreprises do
    post 'hide_case', on: :member
    post 'unhide_case', on: :member
    post 'create_tenant', on: :member
   end

  root to: "pages#home"

My entreprise controller is

class EntreprisesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_entreprise, only: [:edit, :update]
  before_action :require_same_user, :only => [:update]

  def index
    @entreprises = current_user.entreprises
  end

  def new
    @entreprise = current_user.entreprises.build
  end

  def create
    @entreprise = current_user.entreprises.build(entreprise_params)
    @entreprise.owner_id = current_user.id

    if @entreprise.save
      redirect_to entreprise_listing_path, flash[:notice] = "Saved..."
    else
      flash[:alert] = "Something went wrong...#{@entreprise.errors.full_messages.join('. ')}"
      render :new
    end
  end

  def listing
    @entreprises = Entreprise.where(:owner_id => current_user.id)
  end
 def show
   @entreprises = current_user.entreprises
 end

  def update
    if @entreprise.update(entreprise_params)
      flash[:notice] = "Saved..."
    else
      flash[:alert] = "Something went wrong...#{@entreprise.errors.full_messages.join('. ')}"
    end
    redirect_back(fallback_location: request.referer)
  end

  def hide_case
    @case = Entreprise.find(params[:id])
    @case.update(active_entrerpise: true)
    redirect_to entreprises_listing_path
  end

  def unhide_case
    @case = Entreprise.find(params[:id])
    @case.update(active_entrerpise: false)
    redirect_to entreprises_listing_path
  end

  def create_tenant
    Apartment::Tenant.create(subdmain)
    redirect_to entreprise_path(params[:entreprise])
  end

and my view is listing.html.erb in entreprise folder:

<h2> VOTRE LISTING DES ENTREPRISES </h2>
<div class="row">
  <div class="panel-body">

        <% @entreprises.each do |entreprise| %>
          <div class="row">
            <div class="col-md-5">
              <h4><%= link_to entreprise.name_entreprise, edit_entreprise_path(entreprise) %></h4>
            </div>
            <div class="col-md-2">
            <%= link_to "add new tenant", create_tenant_entreprise_path(id: entreprise.id)  %>
            </div>
              <div>
              <% if entreprise.active_entrerpise? %>
                <%= button_to "Unactivate", unhide_case_entreprise_path(id: entreprise.id), class: "btn btn-normal" %>
              <% else %>
                <%= button_to "Activate", hide_case_entreprise_path(id: entreprise.id), class: "btn btn-normal" %>
              <% end %>
            </div>
          </div>
    <% end %>

  </div>
</div>

1 Answers1

1

I think you should change this line <%= link_to "add new tenant", create_tenant_entreprise_path(id: entreprise.id) %> to <%= link_to "add new tenant", create_tenant_entreprise_path(id: entreprise.id), method: :post %>

You are sending GET request instead of POST.