2013-04-01 55 views
0

奧利爲基礎,但我有一個時間地獄得到一個非常基本的流量的工作:NoMethodError在<myController的>#<myMethod>未定義的方法`<myModuleMethod>」爲<myModule>

1)定義模塊與保存url到VAR(或歸還)

2)調用該方法在控制器初始化方法

3)有一個觀點顯示了一種方法,URL

NoMethodError在AuthController #oauth undefined metho d`oauthurl」的GetAccessToken:模塊

模塊:\ 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 

控制器:應用\控制器\ auth_controllers.rb

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

檢視:應用\視圖\ AUTH \ oauth.html.erb

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

我更高層次的目標是讓ETRADE OAuth的流動工作,但我婉以確保我理解每一行代碼與接受其他人的代碼,並且我無法獲得這個非常基本的構建模塊的工作。

回答

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 

控制器:應用\控制器\ test_controller.rb

require 'test_module' 

class TestController < ApplicationController 
    include TestModule 

    before_filter :testUrl1Init 

    def testUrl1Init 
    @testurl=TestModule.testUrl 
    end 
end 

查看:\程序\意見\測試\ test.html.erb

<% provide(:title, 'test') %> 
<h1>test</h1> 
<%= link_to "test link", @testurl %> 
0

添加以下的config/application.rb中:

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

您AuthController更改爲:

class AuthController < ApplicationController 
    include GetAccessToken 

    def oauthurl1 
    GetAccessToken.oauthurl 
    end 
end 
+0

謝謝 - 我想這燙髮utation(將配置行添加到application.rb;初始化oauthurl1),但不幸的是,這還沒有爲我工作 - 同樣的錯誤。 –

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

+0

感謝您的快速響應!我搬到了包括和我忘了說我有一個的before_filter:oauthurl1線初始化@oauthurl方法,但我仍然得到我更新的代碼相同的錯誤。 –

+0

我最終使用的東西很接近這個。再次感謝您的幫助! –

相關問題