0

对基础知识感到抱歉,但我很难让一个非常基本的流程工作:

1) 使用将 url 保存到 var 的方法定义模块(或返回它)

2) 在控制器中调用该方法以初始化该方法

3)有一个视图显示该URL

AuthController#oauth 中的 NoMethodError 未定义方法 `oauthurl' 用于 GetAccessToken:Module

模块:\lib\get_access_token.rb

module GetAccessToken
    CONSUMER_TOKEN = { :token=>"mytokenstringwhichisreallylong", :secret=> "mysecretstringwhichisreallylong" }
    def self.oauthurl    
      @oauthurl="https://us.etrade.com/e/t/etws/authorize?key=#{(CONSUMER_TOKEN[:token])}&token="
    end
end

控制器:app\controllers\auth_controllers.rb

require 'get_access_token'
class AuthController < ApplicationController
include GetAccessToken
before_filter :oauthurl1
    def oauthurl1
        GetAccessToken.oauthurl
    end
end

查看:app\views\auth\oauth.html.erb

<% provide(:title, 'oAuth') %>
<h1>oAuth</h1>
<%= link_to "oAuth", @oauthurl %>

我更高级别的目标是让 eTrade oAuth 流程正常工作,但我想确保我理解每一行代码而不是使用别人的代码,我还不能让这个非常基本的构建块工作。

4

3 回答 3

0

将以下内容添加到 config/application.rb:

config.autoload_paths += Dir["#{config.root}/lib/**"]

将您的 AuthController 更改为:

class AuthController < ApplicationController
  include GetAccessToken

  def oauthurl1
    GetAccessToken.oauthurl
  end
end
于 2013-04-01T05:26:28.300 回答
0

您的模块代码将是

module GetAccessToken
  CONSUMER_TOKEN = { :token=>"mytokenstringwhichisreallylong", :secret=> "mysecretstringwhichisreallylong" }
  def self.oauthurl    
    "https://us.etrade.com/e/t/etws/authorize?key=#{(CONSUMER_TOKEN[:token])}&token="
  end
end

你的控制器代码应该是

require 'get_access_token'

class AuthController < ApplicationController
  include GetAccessToken

  def oauthurl1
    @oauthurl = GetAccessToken.oauthurl
  end
end

我们需要@oauthurl在控制器中初始化才能在视图中使用这个变量,否则它将是nil.

于 2013-04-01T05:37:43.020 回答
0

在上述贡献的慷慨帮助下,这就是我最终解决错误的方法。我在控制器而不是模型中定义了实例变量,并使用 before_filer 初始化了控制器方法:

型号:\lib\test_module.rb

module TestModule
    CONSUMER_TOKEN = { :token=>"myReallyLongToken", :secret=> "myReallyLongSecret" }

    def self.testUrl
        "https://us.etrade.com/e/t/etws/authorize?key=#{(CONSUMER_TOKEN[:token])}&token="
    end
end

控制器:app\controllers\test_controller.rb

require 'test_module'

class TestController < ApplicationController
  include TestModule

  before_filter :testUrl1Init

  def testUrl1Init
    @testurl=TestModule.testUrl
  end
end

查看:\app\views\test\test.html.erb

<% provide(:title, 'test') %>
<h1>test</h1>
<%= link_to "test link", @testurl %>
于 2013-04-04T05:21:58.617 回答