2013-04-01 11 views
0

에 넣기 위해, 나는 시간의 지옥에 봉착하지만, 아주 기본적인 흐름을 점점하려면 다음NoMethodError <myMethod> 정의되지 않은 메서드`<myModuleMethod> '기본에 대한 <myModule>

1) 모듈을 정의 뷰 쇼를 var에에 URL을 저장 (또는 반환)

2) 방법

3를 초기화하는 컨트롤러에서이 메서드를 호출) 가지고하는 방법과 그 AuthController에서 URL

NoMethodError #oauth undefined metho D GetAccessToken에 대한`oauthurl '모듈

모듈 : \ 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 

보기 : 응용 프로그램 \ 전망 \ 인증 \ oauth.html.erb

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

내 높은 수준의 목표는이 트레이드의 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

추가 다음 설정/application.rb :

config.autoload_paths += Dir["#{config.root}/lib/**"] 
는에 AuthController 변경

:

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 

및 컨트롤러 코드가 될 것이다

class AuthController < ApplicationController 
    include GetAccessToken 

    def oauthurl1 
    GetAccessToken.oauthurl 
    end 
end 
+0

감사합니다 -이 파마를 시도 utation뿐만 아니라 (application.rb에 설정 라인을 추가; oauthurl1 초기화),하지만 그 불행히도 나를 위해 아직 작동하지 않습니다 - 같은 오류. –

0

모듈 코드해야 be

require 'get_access_token' 

class AuthController < ApplicationController 
    include GetAccessToken 

    def oauthurl1 
    @oauthurl = GetAccessToken.oauthurl 
    end 
end 

컨트롤러에서 @oauthurl을 초기화해야 뷰에서이 변수를 사용할 수 있습니다. 그렇지 않으면 nil이됩니다.

+0

빠른 응답을 보내 주셔서 감사합니다! 나는 포함 된 파일을 옮겼고 @authurl 메서드를 초기화하기 위해 before_filter : oauthurl1 행을 가지고 있다는 것을 잊어 버렸지 만 여전히 업데이트 된 코드로 동일한 오류가 발생합니다. –

+0

나는 이것에 꽤 가까운 것을 사용했다. 도와 주셔서 다시 한 번 감사드립니다! –

관련 문제