简体   繁体   中英

uninitialized constant “controllername::modulename” TableauServer

Im trying to test the tableau_trusted.rb example for trusted authentication for tableau server in Ruby on rails but I keep getting the error "uninitialized constantTableauTrustedsController::TableauTrustedInterface", this is my code:

tableautrusteds_controller.rb

class TableauTrustedsController < ApplicationController
  include TableauTrustedInterface

  def index
    tabserver = 'xxxxx'
    tabuser   = 'test'
    tabpath   = 'views/Tableau_DW1/General?:iid=1'
    tabparams = ':embed=yes&:toolbar=no'
    ticket    = tableau_get_trusted_ticket(tabserver, tabuser, request.remote_ip)

    if ticket != "-1"
      url = "http://#{tabserver}/trusted/#{ticket}/#{tabpath}?#{tabparams}"
      redirect_to url
      return
    end

    render :status => 403, :text => "Error with request"
  end
end

module TableauTrustedInterface
  require 'net/http'
  require 'uri'

  # the client_ip parameter isn't necessary to send in the POST unless you have
  # wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
  def tableau_get_trusted_ticket(tabserver, tabuser, client_ip)
    post_data = {
        "username" => tabuser,
        "client_ip" => client_ip
    }

    response = Net::HTTP.post_form(URI.parse("http://#{tabserver}/trusted"), post_data)

    case response
      when Net::HTTPSuccess
        return response.body.to_s
      else
        return "-1"
    end
  end
end

I have changed the line "include TableauTrustedInterface" to "extend TableauTrustedInterface" but it didn't work. Also, The URL I put in the browser is http://localhost:3000/tableautrusteds/index , I use get 'tableautrusteds/index' in routes.rb. I don't really know if that is important but some people ask me for this.

I am little bit new in rails so any help will be very appreciated.

I fixed my problem, if anybody was having a similar issue here is my code

module TableauTrustedInterfaces
  require 'net/http'
  require 'uri'

  # the client_ip parameter isn't necessary to send in the POST unless you have
  # wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
  def tableau_get_trusted_ticket(tabserver, tabuser, client_ip)
    post_data = {
        "username" => tabuser,
        "client_ip" => client_ip
    }

    response = Net::HTTP.post_form(URI.parse("http://#{tabserver}/trusted"), post_data)

    case response
      when Net::HTTPSuccess
        return response.body.to_s
      else
        return "-1"
    end
  end
end

class TableauTrustedController < ApplicationController
  include TableauTrustedInterfaces

  def index
    tabserver = 'xxxxx'
    tabuser   = 'test'
    tabpath   = 'views/Tableau_DW1/General?:iid=1'
    tabparams = ':embed=yes&:toolbar=no'
    ticket    = tableau_get_trusted_ticket(tabserver, tabuser, request.remote_ip)

    if ticket != "-1"
      url = "http://#{tabserver}/trusted/#{ticket}/#{tabpath}?#{tabparams}"
      redirect_to url
      return
    end
    render json: {}, status: :forbidden
  end
end

In order to use the module it needs to be declared before the class. Also, and very important I changed the name of the file to tableau_trusted_controler.rb because the snake case that rails uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM