I'm upgrading an old Rails 2 app to 3.2. Rspec is giving me this error:
expected /app/models/api/key.rb to define Api::Key
The actual file is:
module API
class Key
So the capitalization is wrong according to Rails convention. I'd like to avoid project wide searching and trying to change everywhere the constant is referenced. Is there any way to tell Rails the module is in all capitals?
For reference, I did try to use the inflector:
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "API"
end
Is it a caps issue? You have "API" and the error says its expecting "Api". I'm not sure if that would matter or not but it seems like it would
Related
We are currently using Ruby 1.9.3, Rails 3.1 (i know, we're working hard to upgrade all our applications).
We're using a module (let's call it 'OurModule' to add a method (let's call it 'OurAddOnMethod' to a model defined in a gem (let'd call that 'GemModel'). We have that module file living in the 'config/initializers' directory.
That file defines the module, and then calls this to include it in the model:
# Include the extension
GemModel.send(:include, OurModule)
When developing, things work well mostly, but periodically we will get an error that basically says "Undefined method 'OurAddOnMethod' in 'GemModel'". Restarting the server resolves the issue (for a while).
I'm assuming this is happening because the models are reloaded periodically with changes made in the development environment, and it appears that the initializers do not also get reloaded at that time..? It seems like this may not be the best way to set things up; it is quite frustrating to deal with.
Can anyone enlighten me on a better way to achieve this?
I ended up using wrapping the code in the following, and keeping it in initializers:
ActionDispatch::Callbacks.to_prepare do
# configure stuff or initialize
end
I feel really bad, i completely missed this question that seems to completely cover mine (linking to the answer that i used):
https://stackoverflow.com/a/8636163/287516
I'm using the gem rack-google-analytics in my rails project but when I run it in production mode I get an error.
rack-google-analytics-0.11.0/lib/rack/google-analytics.rb:11:in `initialize': Tracker must be set! (ArgumentError)
I'm trying to find out what this means. initialize': Tracker must be set!
in my application.rb file I have this at the bottom of it.
if Rails.env == "production"
config.middleware.use("Rack::GoogleAnalytics", :web_property_id => "UA-18760745-1")
end
If O take that out the error goes away so it has something to do with initializing this behavior, but just not quite sure why.
Anyone ran into this issue and have a solution to it, that they could share.
The only answer I could find was to not use that Gem and use this one instead as it works right out of the box. Maybe someone else can prove me wrong, which I hope.
Hope this other link helps someone else.
https://github.com/bgarret/google-analytics-rails#readme
Don't know if it helps, but here is my code:
config.middleware.use Rack::GoogleAnalytics, :tracker => 'UA-XXXXXXXX-1'
in application.rb
I'm assuming it requires a value for the :tracker symbol upon initialization (I'm following this readme on the gem's GitHub: https://github.com/kangguru/rack-google-analytics)
Obviously, replace XXXXXXXXX with your organization's tracker code.
And it seems like setting the :web_property_id symbol is a feature of a different (but similarly named) gem, rack-google_analytics: https://github.com/ambethia/rack-google_analytics
I have a feeling this is a dumb one, but I've spent some time trying to figure it out and googling it, and to no avail. I'm trying to use a Ruby Gem in a Controller. I have included it in my Gemfile, run bundle install, seen it show up in my gems list, restarted my local server. But somehow whenever I try to call the gem ( rails_rrdtool ) It just tells me
uninitialized constant RrdgraphsController::RRD
app/controllers/rrdgraphs_controller.rb:22:in `show'
The spot in my code where it wigs is when I'm calling
RRD.graph
It's as though it doesn't know where the heck the gem is... However, I can use require to import it successfully into an irb session. So I know that it works, it's just not getting in there some how...
Bundler should be handling the inclusion of the gem I assume. Am I calling it in the wrong place?
This looks like a namespacing issue. Your error says it is looking for the constant inside of the current class: RrdgraphsController::RRD when it should be looking for a class outside of the current context.
Try prefixing the class name with a double colon to fully define the location of the class.
::RRD.graph #rest of your code
There's a good analogy of what this does in this other accepted answer. Basically it creates an absolute path so Ruby doesn't have to guess.
A simple question. Does this version support generators?
I wanted to test out using it and follow along with Ryan Bates screen cast which I uses a different version.
I've been playing with tables today and want to see how this works using the generator with a scaffold generated model and all its components.
Running rails g bootstrap:themed returns
Could not find generator bootstrap:themed
So I tried to reinstall with rails g install:bootstrap
Error similar which lead me to try to find if it supports these commands.
Thanks
It doesn't need generators for asset files, since we hook into the asset pipeline through the use of a Rails Engine - configuration options are available through the use of variables (use this as a reference, Sass variables are actually $x rather than #x and need to be defined before importing bootstrap), Sass' #extend, and Bootstrap's #makeRow and #makeColumn mixins, along with the other Bootstrap mixins.
Themed scaffold would be interesting but generally would be a pain to maintain - view scaffolding tends to get ripped apart pretty quickly anyway. Perhaps a 'sane' application.html.erb layout generator could be useful.
So yeah, we currently have no generators, don't need an asset one, themed scaffold probably not coming soon unless someone is interested enough to do the work on it, potentially a layout generator in the pipeline.
Checkout
https://github.com/decioferreira/bootstrap-generators
Seems to be what you are asking for.
-Rick
I am a beginner in learning Ruby and Rails.
I was going through the following section on Rails 3.2.1 Guide:
http://guides.rubyonrails.org/routing.html#paths-and-urls
which says:
Creating a resourceful route will also expose a number of helpers to the controllers
in your application. In the case of resources :photos:
photos_path returns /photos
new_photo_path returns /photos/new
edit_photo_path(:id) returns /photos/:id/edit (for instance, edit_photo_path(10) returns /photos/10/edit)
photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)
I am curious to know how and when in the lifecycle of a request are these helpers, i.e. new_photo_path, edit_photo_path etc created and where in the source code I can found the code doing the same.
I was navigating through the code in the following file /gems/actionpack-3.2.1/lib/action_dispatch/routing/mapper.rb and I guess the code in this file is creating the above helpers.Please correct me if I am wrong.
Thanks,
Jignesh
Essentially, yes, that is correct. For more information, you should check out the Routing Walkthrough series that Ryan Bates did last September, where he walks through some of the Rails code that controls routing.
If you use the generators (e.g. rails g controller NewController) the helpers are automatically created and places in ./app/helpers with similar nomenclature. If you are doing the controllers by hand then you would need to create your own, e.g. new_helper. By default though all controllers load application_helper.rb.
If you are not going to use a helper method in more than one controller/view it is best to put it in its own helper file. This guide may help explain it more.
Update
The helper methods are written by you based on your needs. The code for the helper generator can be found here