2015-04-27 58 views
0

我遵循此tutorial及其正常工作。rails NoMethodError:未定義的方法`helper_method'爲ApplicationHelper:模塊

[以下答案之後更新] 我提出的代碼到應用控制器(先前定義爲助手),以確定當前用戶是否被記錄在

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 :current_user 

    def current_user 

    @current_user ||= User.find(session[:user_id]) if session[:user_id] 

    end 
end 

我已經創建了一個模塊來創建一個客戶端對象並進行一次api調用,這個功能可能被一個或多個對象使用,因此將它創建爲模塊而不是控制器似乎是一個好主意。

require 'base64' 
    require 'rubygems' 
    require 'json' 
    require 'google/api_client' 
    require 'google/api_client/client_secrets' 
    require 'net/https' 
    require 'uri' 

module GoogleClient 

    include ApplicationController 


    PLUS_LOGIN_SCOPE = 'https://www.googleapis.com/auth/plus.login' 

    # Build the global client 
    $credentials = Google::APIClient::ClientSecrets.load("#{Rails.root}/config/client_secret_xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com.json") 
    $authorization = Signet::OAuth2::Client.new(
    :authorization_uri => $credentials.authorization_uri, 
    :token_credential_uri => $credentials.token_credential_uri, 
    :client_id => $credentials.client_id, 
    :client_secret => $credentials.client_secret, 
    :redirect_uri => $credentials.redirect_uris.first, 
    :scope => PLUS_LOGIN_SCOPE) 
    $client = Google::APIClient.new(options = {:application_name => 'xxx-xxx-xxx'}) 

    def GoogleClient.get_people 

    if current_user 

     # Authorize the client and construct a Google+ service. 
     $client.authorization.update_token!(current_user.oauth_token.to_hash) 
     plus = $client.discovered_api('plus', 'v1') 

     # Get the list of people as JSON and return it. 
     response = $client.execute!(plus.people.list, 
     :collection => 'visible', 
     :userId => 'me').body 
     content_type :json 
     puts response 

    else 

     redirect_to root_url 

    end 
    end 
end 

用戶模型是;

require 'google_client' 


class User < ActiveRecord::Base 

    include GoogleClient 

# after_save GoogleClient.connect 


    def self.from_omniauth(auth) 

    where(provider: auth["provider"], uid: auth["uid"]).first_or_initialize.tap do |user| 

     user.provider = auth.provider 

     user.uid = auth.uid 

     user.name = auth.info.name 

     user.email = auth.info.email 

     user.oauth_token = auth.credentials.token 

     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 

     user.save! 

    end 

測試它在控制檯結果

2.1.0:001> GoogleClient.get_people NoMethodError:未定義的方法`是helper_method」的ApplicationHelper:模塊

是否有可能調用到輔助方法在模塊中?我應該如何實現這個代碼,如果一個模塊是不正確

**更新正確的模塊代碼,但在這個post

「注意,模塊/ lib中的API請求重定向URI錯誤**解釋這裏不。自動加載相反,你需要在你的配置加入這一行/ application.rb中文件的文件配置塊:」

config.autoload_paths += %W(#{config.root}/lib) 

用戶模型

class User < ActiveRecord::Base 

    include Google::GoogleClient 

    def self.from_omniauth(auth) 

    where(provider: auth["provider"], uid: auth["uid"]).first_or_initialize.tap do |user| 

     user.provider = auth.provider 

     user.uid = auth.uid 

     user.name = auth.info.name 

     user.email = auth.info.email 

     user.oauth_token = auth.credentials.token 

     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 

     user.save! 

    end 
    end 

    if current_user 

    self.get_people() 

    else 

    redirect_to root_url 

    end 
end 

'的lib /谷歌/ google_client.rb'

require 'google/api_client' 
require 'google/api_client/client_secrets' 
require 'google/api_client/auth/installed_app' 

module Google 

    module GoogleClient 

    # Initialize the client. 
    client = Google::APIClient.new(
     :application_name => 'xxx-xxx', 
     :application_version => '1.0.0' 
    ) 

    # Initialize Google+ API. Note this will make a request to the 
    # discovery service every time, so be sure to use serialization 
    # in your production code. Check the samples for more details. 


    # Load client secrets from your client_secrets.json. 
    client_secrets = Google::APIClient::ClientSecrets.load("#{Rails.root}/config/client_secret_XXXXXXXXXXXXXXXXXXXXXXXx.apps.googleusercontent.com.json") 


    # Run installed application flow. Check the samples for a more 
    # complete example that saves the credentials between runs. 
    flow = Google::APIClient::InstalledAppFlow.new(
     :client_id => client_secrets.client_id, 
     :client_secret => client_secrets.client_secret, 
     :scope => ['https://www.googleapis.com/auth/plus.me'] 
    ) 
    client.authorization = flow.authorize 

    def get_people 

     # Make an API call. 
     result = client.execute(
     :api_method => plus.activities.list, 
     :parameters => {'collection' => 'public', 'userId' => 'me'} 
    ) 

     puts result.data 

    end 
    end 
end 

回答

0

移動這兩個到ApplicationController中:

def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
end 

helper_method :current_user 

這會給你一個current_user方法,在控制器,助手,並享有工作。

+0

在ApplicationController中定義方法並從ApplicationHelper中刪除後出現相同的錯誤 – Conor

+0

ApplicationHelper中仍然存在'未定義的方法helper_method?如果是這樣,你仍然試圖在助手中調用'helper_method'。它只能在控制器中調用。 – infused

+0

正確,改爲包括ApplicationController中,但現在新的錯誤 '2.1.0:001> GoogleClient.get_people 類型錯誤:錯誤的參數類型類(預期模塊) ' 我應該把這個在一個控制器,因爲它依賴在其他控制器方法? – Conor

相關問題