I am trying to learn Ruby on Rails by playing around with stuff and I'm attempting to play around with Grit. However I am a bit confused coming from a PHP background where I get the repo stuff from. My code
class RepoController < ApplicationController
require "grit"
repo = Grit::Repo.new("blahblahblah")
def index()
puts YAML::dump(repo)
end
def show()
repo.commits('master', 10)
puts repo.inspect
end
end
I am trying to dump out information on the objects, but I cannot seem to access the repo variable. My IDE and Ruby keep saying undefined local variable or method repo'
and I don't know why it cannot access the repo variable, it's declared at the top of the class?
You've got scope problems. Try:
require 'grit'
class RepoController < ApplicationController
def repo
@repo ||= Grit::Repo.new("blahblahblah")
end
def index()
puts YAML::dump(repo)
end
def show()
repo.commits('master', 10)
puts repo.inspect
end
end
Your repo variable is being defined out of the scope that that is visible in your index and show actions. Probably what you want is something like this:
class RepoController < ApplicationController
before_filter :set_repo
def index()
puts YAML::dump(@repo)
end
def show()
@repo.commits('master', 10)
puts @repo.inspect
end
def set_repo
@repo = Grit::Repo.new("blahblahblah")
end
end
That creates an instance variable when the controller is loaded. Additionally you will want to get that require statement out of there and put gem "grit" in your Gemfile instead.