Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: controller method or instance variable inside a helper

I'm using the bitly gem and would like to have access to the bitly API inside my helper methods (which get called by views and mailers to generate URLs).

I initiate an API connection in this method in my ApplicationController:

(is there a more appropriate place to do this BTW?)

class ApplicationController < ActionController::Base
  before_filter :bitly_connect

  def bitly_connect
    Bitly.use_api_version_3
    @bitly ||= Bitly.new(APP_CONFIG['bitly_username'], APP_CONFIG['bitly_api_key'] )
  end
end

By default I don't have access to @bitly in my helpers. Can you suggest a way to accomplish that?

The only related thread I found wasn't helpful: Rails 3 and Controller Instance Variables Inside a Helper

Thanks.

like image 947
stupakov Avatar asked Sep 25 '11 01:09

stupakov


2 Answers

Rails by convention passes instance variables set in the controller actions (and filters) along to the views. The helper methods are available in these views, and should have access to the instance variables you set inside your controller action.

Alternately, you can set a local variable inside your helper method by passing the variable to the method, or by using the Object#instance_variable_get method: http://ruby-doc.org/core/classes/Object.html#M001028

# app/controllers/example_controller.rb
class ExampleController
  def index
    @instance_variable = 'foo'
  end
end

# app/helpers/example_helper.rb
module ExampleHelper
  def foo
    # instance variables set in the controller actions can be accessed here
    @instance_variable # => 'foo'
    # alternately using instance_variable_get
    variable = instance_variable_get(:@instance_variable)
    variable # => 'foo'
  end
end

As for your concerns with the placement of the logic, it does not look like it belongs in the controller. Think of the controller as routing requests for your application. Most logic should be performed inside of your model classes. "Skinny controller, fat model.": http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model

like image 83
Ben Simpson Avatar answered Oct 12 '22 07:10

Ben Simpson


If you need a controller method to be accessible as a helper, you can use helper_method

class ApplicationController < ActionController::Base
  helper_method :bitly_connect

  def bitly_connect
    @bitly ||= begin
      Bitly.use_api_version_3
      Bitly.new(APP_CONFIG['bitly_username'], APP_CONFIG['bitly_api_key'] )
    end
  end
end

Note that I also altered the method, so that it doesn't call Bitly.use_api_version_3 each time it is called.

As Ben Simpson noted, you should probably move this into a Model though.

like image 23
axelarge Avatar answered Oct 12 '22 07:10

axelarge