Question

In my rails 4 app I'm having trouble extracting the twitter gem config from my controller to a module, getting

undefined method `include' for #<UsersController:0x007ff7d566df08>

Users_controller.rb

def show
    include Twitconfig
    ...
end

controllers/concerns/Twitconfig.rb

require 'twitter'
module Twitconfig
    @client = Twitter::REST::Client.new do |config|
    ...
    end
end

I've tried moving the "include Twitconfig" to out of the new action like so

class UsersController < ApplicationController
    include Twitconfig

but that just gave an undefined method error when calling @client.

This is my first time including a module in rails 4 and I've been trying for a while so any help would be really appreciated.

Was it helpful?

Solution

The problem is your module not the way you include it, you cannot write code outside a method.

Include will add instance method to a class, so you should try with :

require 'twitter'
module Twitconfig
    def client
      client = Twitter::REST::Client.new do |config|
      ...
      end
    end
end

And in your controller :

class UsersController < ApplicationController
    include Twitconfig


def show
    puts "#{client.inspect} 
end

It should display your client

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top