0

I m creating a tenant when I click on a button in a view. The tenant is well create but when I try to switch to this tenant just after I have this error: LocalJumpError in EntreprisesController#create_tenant - no block given (yield). Thanks for any help

My controller:

# creation du tenant dans apartement
  def create_tenant
    Apartment::Tenant.create(params[:id])
    Apartment::Tenant.switch(params[:id])
 #   redirect_to entreprise_path(enterprise_id: params[:entreprise])
  end

My view:

  <div class="col-md-2">
          <%= link_to "add new tenant", create_tenant_entreprise_path(id: entreprise.subdmain), method: :post  %>
        </div>
  • Can you tell what is `switch` in `Apartment::Tenant.switch(params[:id])` I have never seen this kind of code yet. I was thinking of a switch method in Tennant controller but not sure at all. – Maxence Jul 19 '18 at 23:31
  • Sorry didn't see it was Apartment gem related. Not an easy gem to work with as a Rails beginner, especially if you have a User domain working with Devise. You should watch this video which seems pretty good https://youtu.be/L5y72hfIT_M . The video says that basically you shouldn't redirect, but let your user type his address like `newtenant.myapp.com` to get to the very tenant. In your case your are missing the block. Apartment documentation says you can use `switch!` instead. It should work then. – Maxence Jul 21 '18 at 01:05

1 Answers1

2

You don't need the switch at all, just redirect to the proper url.

If you have set a subdomain apartment, then you have to redirect to that subdomain. Apartment elevator will do the switch automatically from the url as is always the case.

switch is meant to be used with a block. There's a switch! that might work in your case too, but this is not the way to go unless you have some very specific edge case, which I honestly can't think of.

estani
  • 24,254
  • 2
  • 93
  • 76