rails config/environments where is 'config' defined? - ruby-on-rails

In config/environments/production.rb (and the other environments) there is:
config.eager_load = true
and a whole bunch of other config.foobar calls. But where does 'config' come from? Usually you have something like:
SomeClass.each do | block_variable |
block_variable.some_method
end
but in the case of the mystical 'config' this is not the case, its like a block variable which is not declared anywhere. Opening rails console, when I do:
irb(main):001:0> config
NameError: undefined local variable or method `config' for main:Object
Did you mean? conf
from (irb):1
irb(main):002:0>
and the same result occurs for app.config
irb(main):002:0> app.config
NoMethodError: undefined method `config' for #<ActionDispatch::Integration::Session:0x00007fc898d02808>
from (irb):2
How is it possible for ruby to allow calls on 'config'?

These classes include the ActiveSupport::Configurable module:
https://api.rubyonrails.org/classes/ActiveSupport/Configurable.html
Here is an alternative to implementing this behavior by yourself:
https://robots.thoughtbot.com/mygem-configure-block

Related

Setting up mime_types.rb in rails

I am trying to use Prawn in RoR 4.2.0. I am trying to add the following to my mime_types file:
Mime::Type.register “application/pdf” , :pdf
However, the server does not start with the following error:
/Users/Bob/Development/appname/config/initializers/mime_types.rb:7:in `<top (required)>': undefined local variable or method `“application' for main:Object (NameError)
Any thoughts?
Replace the curly quotes in “application/pdf” with straight quotes (i.e. "application/pdf")

Httparty in rails

I am trying to learn how to use httparty. I ran 'gem install httparty', first in my terminal, expecting to be able to use it in a pry session but no luck.
Next, I created a new rails app, added the gem to my gem file and ran bundle and in a pry session tried to use httparty as follows:
[1] pry(main)> HTTParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: uninitialized constant HTTParty
from (pry):1:in `__pry__'
[2] pry(main)> HTTParty.get("http://rubygems.org/api/v1/versions/httparty.json)
[2] pry(main)* httParty.get("http://rubygems.org/api/v1/versions/httparty.json)
SyntaxError: unexpected tIDENTIFIER, expecting ')'
httParty.get("http://rubygems.org/api/v1/versions/httparty.json)
^
[2] pry(main)> httParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: undefined local variable or method `httParty' for main:Object
from (pry):2:in `__pry__'
[3] pry(main)> response = httParty.get("http://rubygems.org/api/v1/versions/httparty.json")
NameError: undefined local variable or method `httParty' for main:Object
from (pry):3:in `__pry__'
[4] pry(main)> response = HTTParty.get('https://api.stackexchange.com/2.2/questions?site=stackoverflow')
NameError: uninitialized constant HTTParty
from (pry):4:in `__pry__'
Any help would be much appreciated. Thank you
require the gem, either in your pry invocation
pry -rhttparty
or once in pry
require 'httparty'
I am working on the exact same thing right now. What I did was create a file test_party.rb in the rails lib directory. (Make sure to add config.autoload_paths += %W(#{config.root}/lib) to your config/application.rb
In the new lib file create a class TestParty and include HTTParty
Then in rails console you can run TestParty.whatever_you_want
Hope that helps!

What does uninitialized constant mean?

I'm getting the error uninitialized constant SessionsController I've searched, and can only find explanations of this error in reference to a NameError Does anyone know what the error means?
That is one of two "subtypes of" Name Error dealing with uninitialized variables. The language of "uninitialized constant" is due to the fact that SessionsController is capitalized. Both types are illustrated below:
new-host-3:bot palfvin$ irb
2.0.0p247 :001 > foobar
NameError: undefined local variable or method `foobar' for main:Object
from (irb):1
from /Users/palfvin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'
2.0.0p247 :002 > Foobar
NameError: uninitialized constant Foobar
from (irb):2
from /Users/palfvin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:16:in `<main>'
2.0.0p247 :003 >
It means that you're trying to use a Class or a Module that wasn't defined yet. Probably because you forgot to require them.
Make sure that this class SessionsController has been declared in your sessions_controller.rb.
Read more: http://ruby.about.com/od/faqs/qt/Nameerror-Uninitialized-Constant-Object-Something.htm

How to set up rack-offline in rails 3.2 app

How to make rake-offline work in rails 3.2.11 ?
I added initializer
offline = Rack::Offline.configure do
#cache "images/masthead.png"
public_path = Rails.public_path
Dir[public_path.join("javascripts/*.js")].each do |file|
cache file.relative_path_from(public_path)
end
network "/"
end
I added in routes
match "/application.manifest" => Rails::Offline
Rack::Offline.configure do
cache "assets/application.js"
cache "assets/application.css"
network "/"
end
and added manifest in html tag.
It throws error
/initializers/offline.rb:5:in `block in <top (required)>': undefined method `join' for "/Sites/Ruby/project/public":String (NoMethodError)
In Rails 3.2.11, Rails.public_path returns a String, not a Pathname object. (It looks like Rails master has it returning a Pathname object which is why rack-offline's documentation might say to use it that way).
Try this instead:
public_path = Pathname.new(Rails.public_path)
See https://github.com/wycats/rack-offline/issues/7

NameError: undefined local variable or method `params' for main:Object

I'm using carrier wave and i get
NameError: undefined local variable or method `params' for main:Object
when i do u.commentfile = params[:file]
in the console? help!
The error is telling you exactly what's wrong. In the console, you're not in the scope of a request, so the params variable isn't defined unless you've done so manually. What are you trying to do?

Resources