jeudi 27 juillet 2017

Devise User:edit not accepting update of subdomain | RAILS | DEVISE | devise-basecamper

So I have added a new subdomain functionality to my app that's running on heroku

gem 'devise'
gem 'devise-basecamper'

Each User has a subdomain specific for him such as: User.domain.com

Since I have old users I created a temporary functionality to let the old users add a subdomain to their user Account.

But the problem is doing that the Postgresql database is not capable of finding the user to update him:

Here's the error log here

and my application controller

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :subdomain, :current_account
before_filter #:validate_subdomain #, :authenticate_user!

before_action :configure_permitted_parameters, if: :devise_controller?
 before_filter :set_search

    def set_search
    @q = User.ransack(params[:q])
    @users = @q.result(distinct: true).page(params[:page]).per(10)
    end


protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up).push(:name, :subdomain)
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :current_password, :avatar, :memo, :password_confirmation, :subdomain) } #<< :name
end

private # ----------------------------------------------------

def invalidUser
    # The where clause is assuming you are using Mongoid, change appropriately
    # for ActiveRecord or a different supported ORM.
    #@current_user ||= Association.where(subdomain: subdomain).first
    User.where("subdomain = ?", subdomain).first
end

def invalidsubdomain
    case subdomain
        when 'www', '', nil
          false
        else
          true
    end
end 

def subdomain
    request.subdomain
end

# This will redirect the user to your 404 page if the account can not be found
# based on the subdomain.  You can change this to whatever best fits your
# application.
def validate_subdomain
        if (invalidUser.nil? && invalidsubdomain)
            redirect_to '/404.html' 
        end             
end

end 

And my routes.rb

Rails.application.routes.draw do


get '/confessions/:id',     to: 'users#index',       via: 'get'
resources :confessions

devise_for :users, :controllers => { :omniauth_callbacks => 
"users/omniauth_callbacks" ,:registrations => "users/registrations" }


 resources :users, :only => [:index, :show]

 constraints(Subdomain) do
 match '/' => 'users#show' , via: 'get'
 end

 root to: 'pages#index'
 match '/users',   to: 'users#index',   via: 'get'
 match '/users/:id',     to: 'users#show',       via: 'get'

 get '/myconfessions', to: 'pages#myconfessions', as: :myconfessions
 get '/search', to: 'pages#search', as: :search

 end

And finally my lib/subdomain.rb

class Subdomain
  def self.matches?(request)
    case request.subdomain
    when 'www', '', nil
      false
    else
      true
    end
  end
end

Thank you prior! any help would be wonderful. I've been stuck on this for a while

Aucun commentaire:

Enregistrer un commentaire