Where can I look at the Rails controller high-level superclasses? - ruby-on-rails

I am trying to understand some of the higher level functioning of Rails, using the Rails console. I run controller.class.superclass.superclass which gives ActionController::Base, controller.class.superclass.superclass.superclass which gives ActionController::Metal and controller.class.superclass.superclass.superclass.superclass gives AbstractController::Base.
I have found these in the API documentation.
http://api.rubyonrails.org/classes/AbstractController/Base.html
http://api.rubyonrails.org/classes/ActionController/Metal.html
[can only post two links]
I can add to these simply by declaring the classes again in the console, but is there a way to find the original Ruby code for these and to inspect and edit it in its original file(s)? Just in case I need to know the full contents of these for future.

You can also do:
bundle show <gem>
and that will show you where the gem is on your system. Editing in those files is not advised unless you know how to re-install gems.

You can see the Rails source code on Github:
https://github.com/rails/rails/tree/master/actionpack/lib/action_controller

Related

Ruby on Rails - In what file do I add the following: require 'forecast_io'?

Thanks for your help,
I believe this is very simple but I can't figure it out. Following the instructions at https://github.com/darkskyapp/forecast-ruby - it tells me to not forget to add require 'forecast_io' - what file do I put this in?
I've run a scaffold to set up a simple lat and lng, following this guide:
https://campushippo.com/lessons/an-easy-way-to-implement-weather-forecasting-in-rails-9d10403 but keep on getting method errors. So I think we are using different versions of Ruby & Rails, and/or I'm placing this code in the wrong file, or wrong place. My question isn't about method errors, but just where to place this.
I'm unsure whether or not to place it in /config/application.rb; create and place the code in /config/forecast_io.rb; or to create and place it in /config/initializers/forecast_io.rb; or if it's supposed to go somewhere else entirely.
I've looked at the api docs, the ruby wrapper read me, and also have read other tutorials (they won't let me post more links, otherwise I would list them) - one is from hackpsu.westarate that is kind of different with using sinatra, went through the Treehouse tutorial on Rails scaffolding, and began their tutorial on creating an API to better understand REST, and have looked at other rails projects to see how they incorporate external API's, so I'm in the thick of it, and am banging my head against the wall because it seems so simple, but I'm not seeing it yet.
Thanks for your help!
David
In Rails, placing a gem in your Gemfile already requires the gem by default, unless explicitly stated to ignore.
If you're still bothered about it though, you could add the require option to your gem as such:
gem 'forecast_io', require: 'forecast_io'
Now, for the rest of the configurations mentioned, you can create a file in your initializers and the following:
#config/initializers/forecast_io.rb
ForecastIO.configure do |configuration|
configuration.api_key = 'this-is-your-api-key'
end

Rails 4.2 + Ember => unexpected identifier

I'm following Vic Ramon's tutorial. I tried to use the latest version of Rails and Ember-source and I get the following error when visiting home page:
After clicking on link next to error I get this:
How can I fix that?
I ran the following commands:
rails g ember:bootstrap -n App --javascript-engine coffee
rails g ember:install
I removed turbolinks. I also created home controller and an empty view for home#index. Root is set to home#index. Also created following view file:
// app/assets/javascripts/templates/application.js.emblem
h1 Hello World
outlet
Ember gems im using:
Using emblem-source 0.3.18
Using ember-data-source 1.0.0.beta.14.1
Using ember-rails 0.16.1
Using emblem-rails 0.2.2
This could be due to some version incompatibilities between your versions of ember-related gems, as there were quite substantial changes in recent versions of ember (e.g. introduction of HTMLBars, etc.)
Your best bet is to clone the original repo and then try to upgrade individual gems and check if it still works after each upgrade. This way you will be able to identify the troubling gem.
I have verified that the original repo code indeed works with the versions of gems in its current Gemfile.lock.
If you are starting a new Rails + Ember app, ember-cli-rails is probably the way to go, giving you the best of both worlds.
Emblem was the culprit. Replacing .emblem with .handlebars fixed the problem. I tried the tip from emblem-rails git page and icluded
gem "emblem-source", github: "machty/emblem.js"
but that didn't change anything.
I also followed this tutorial (alongside a few others) and I encountered several issues alongside the way ranging from handling templates to setting up the JSON API. I don't see your entire source code so I cannot pinpoint the exact problem. However, it seems that we have similar projects, so you can compare your code to my blog project source code and see what is causing these issues. Comparing to Ramon's tutorial, my blog has implemented everything until chapter 15.
https://github.com/Deovandski/Fakktion/tree/Ember-Rails
If you are a beginner like myself, then I recommend avoiding CoffeeScript and Emblem and sticking to Javascript as much as possible for the first months because you will find more resources and examples. Also, let me know if you need assistance in setting up the project as I used .gitignore to hide things like secrets.yml

Debugging in rails 3

i want to debug ROR without going through the effort of putting inspect method for every relevant object in the controller as well in the model.is there a better way as we have in Java (Run time debugger using eclipse).i know that I can Use Rails.logger and also make use of rails Console(irb`).i am even aware of debugging/inspecting elements in erb/rb file.Still is there a better,quick and reliable way to debug a Rails app.
There is much better, see this railscats.
It presents two great gems, especially Better Errors
Otherwise, you could use pry with rails, see this railscast.
you can also use pry-rails, pry-debugger and then use binding.pry method in your code and then while using your app you have Rails console available in rails server
Add this lines to your application's Gemfile
group :development do
gem 'ruby-debug19'
end
then run cammand
bundle install
add debugger within your controller or model method, stop the rails server and restart again. Whenever rails found word debugger it stops control at that point. You can easily debug your value or object.
Hope this will helps you.

Ruby on Rails: How do I use a Gem? (Vacuum Amazon API)

I found an Amazon Gem that I want to use https://github.com/hakanensari/vacuum/.
I've only ever used gems with in depth tutorials or following a RailsCast. I'm looking for tips on using gems I find online. I'll explain the steps I'm doing, and hopefully someone can give me some ideas on what else I should be doing when using a new gem. Also, if you have a good tutorial or explanation on gems, that would be great too.
I started off examining the Gem on Github, I'll point out the things I took notice of. Let me know if there are things I'm missing I should notice.
Examining Gem on Github
Go to the examples and look at "examples/product_advertising/basic_lookup.rb"
Follow the required file, and checkout "examples/product_advertising/shared.rb"
Notice, I need to install "pry"
Notice, the examples expand on the "lib" folder
Check out "credentials.yml"
Notice a "#req" is instantiated as a new Vacuum object.
Then back in basic_lookup.rb, it looks like it's assigning the lookup value, then binding the response to some kind of "pry" view.
Next, I'll try implementing these examples in my own project. Here's where I'm not sure what to do as far, as files are concerned.
Attempt Implementing Example
Install vacuum gem
gem install vacuum
Install pry gem
gem install pry
Added "shared.rb" and "credentials.yml" to my "app/controllers" directory
Replaced the information in "credentials.yml" with my information
Attempt to copy the information from "basic_lookup.rb" into an existing controller
def amazon
require File.expand_path('../shared.rb', __FILE__)
res = #req.look_up '0816614024'
items = res.find 'Item'
binding.pry
end
Create a route
match '/test' => 'products#amazon'
Go to test page and receive the following error
undefined method 'look_up' for nil:NilClass
I would like to point out, at this point I haven't added the lib folder.
Questions
I like the credentials.yml is separated out, when I want to add this to my project, where should I save that file?
I like the shared.rb file, should I just put that in the controller folder?
Why is it referencing the "lib" folder in "shared.rb"? Do I need to copy that directory into my project?
I appreciate you sticking around and reading all of this. I'm still trying to get a handle on using a gems, so any help or tips are great. Really, I'm just trying to figure out, how do I find any gem and start using it appropriately.
Thanks for any help you can give me!
I like the shared.rb file, should I just put that in the controller folder?
Answer = yes, you just put that file in controller folder.

Cane with Rails

I just discovered the Cane gem but it doesn't work with Rails, there is a way to make it work with Rails?
Update
I'm using rails 3.2.1 and It doesn't produce any error message
I'm the author of this gem.
How do you mean "doesn't work"? It's possible you just write good code :) The default options aren't too strict. Can you please post the output of:
cane --style-glob '**/*.rb' --style-measure 1
This should error on every line in the project, to verify whether cane is "working". And also
find . app
When run from within your rails directory.

Resources