Search code examples
ruby-on-rails-4soundcloudruby-on-rails-plugins

Cannot use gem in rails controller


I'm a rails noob, so any help would be appreciated.

I've installed the soundcloud gem via:

$  gem install soundcloud

Then I added the gem to my Gemfile, and ran the rails bundle install to make sure it is included in my project:

$  bundle install

This output a list of all of the included gems for my project. One of the items towards the end was the soundcloud gem:

$  bundle install
...
Using sdoc 0.4.0
Using soundcloud 0.3.2
Using spring 1.1.2
...
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

Now when I run the rails console in my application directory, I am able to access the soundcloud module as expected:

$ rails console
Loading development environment (Rails 4.1.0)
2.1.1 :001 > SoundCloud
 => SoundCloud
2.1.1 :002 > client = SoundCloud.new(:client_id => 'b2dab282f8bb29467dcb4f5b27f7c075')
 => #<SoundCloud::Client:0x8972f40 ... (successful results here)

But when I try to use the soundcloud module in a controller or view, I get a page with the error:

uninitialized constant SlideshowController::SoundCloud

Here is the code where I try to use the soundcloud module:

helper_method :sc_get_src

def sc_get_src
    @sc_client ||= SoundCloud.new(:client_id => 'b2dab282f8bb29467dcb4f5b27f7c075')
    sound = @sc_client.get('/resolve', :url => @slideshow.sound)
    return sound.uri
end

On the documentation page here the first line in the example is:

require 'soundcloud'

However, if I add that before I use the soundcloud module (in the controller, model, view etc) it throws this error:

cannot load such file -- soundcloud

Please help. Is there another step that I need to take to include the module in my application?

Thank you in advance.


Solution

  • Those problems are often solved by a restart of your rails server. E.g. For Passenger:

    $ touch tmp/restart.txt
    

    Reason is, that gem dependencies are only loaded in the startup phase of your app