ActiveResource error with "wrong constant name ENV.xxx" - ruby-on-rails

I'm consuming a REST endpoint using ActiveResource which has Keys called among others ENV.OM_PRODUCER, ENV.UMS_PRODUCER.
These appear to be causing an issue with my view, I'm getting errors such as:
NameError in AppsController#index
wrong constant name ENV.UMSProducer
There is nothing else in the logs to help me, any suggestions.
Update: I was far from clear earlier.
I am not doing anything with the data yet, in my controller I have:
#apps = App.all
and in the view I have:
<%= #apps.inspect %>
It seems like ActiveResource is interpreting those ENV. keys and its causing an issue. It feels like I need them to be escaped but I don't know how.

Thanks to this post I determined it was due to the full stop in the key rather than the key name. Putting the code suggested in the post into a .rb file in initializers fixed the problem.

Related

Rails 5.0.7.2 MIME::Type Class has no method `lookup`

I am running Rails 5.0.7.2. In my project I have so far always set the mime type like follows:
render :js => File.read(js_file), :content_type => 'application/javascript'
(for example). It appears, however, I shouldn't be doing it this way but by using the Class MIME::Type. On Rails console I can find it and print its methods. I used (MIME::Type.methods - Object.methods).sort. It only gives me three methods: match, simplified and i18n_key. The documentation says there should be methods lookup and lookup_by_extension.
When I try using MIME::Type.lookup('application/javascript') in my code I get the same error: as if the method wasn't defined.
Does anybody know what is the problem? Thanks in advance!
Maxence's comment solved the problem. Typing Mime::Type instead of MIME::Type is all it took. I should have been more careful when reading the docs. Bit awkward that MIME::Type gave me a result other than nil, or else I would have found my error long ago.

How to delete a helpers file in Rails 5?

I'm new to Rails, and after using rails g controller Users to generate a users controller, I decided to remove (i.e., rm) the generated helper in app/helpers/users.rb, because I realized I didn't need it. It seems ugly to keep a bunch of empty files around to me. This broke my app. When I try to visit any page in my, or run a test, I get this error: Couldn't find UsersHelper, expected it to be defined in helpers/users_helper.rb.
I fixed it by manually re-creating that file, but how do I get rid of it? Is that just not supported?
Edits based on questions people asked
The helper does not appear to be referenced explicitly anywhere, grep -Ri UsersHelper . only returns results for the module itself and a bootsnap cache file. The same command with :helper 'user' or 'helpers/users' returns nothing.
If Rails is telling you that it can't find UsersHelper then it means that the module is called somewhere else in the project. When the page shows you the couldn't find message, it should also indicate the offending file where UsersHelper is being included (same with the test output). If you remove this reference, you should have no problem removing the module (or directory).
I sort of figured out the issue, but I still don't understand it.
I am using some url helper functions: user_url(:id) and new_user_url, and these apparently require the helpers to exist even though they are not explicitly defined there. If anyone can explain in more depth what's going on, I'd appreciate it.

Using Carrierwave on a Devise Model

I'm trying to use carrierwave to add profile images to a user model that's handled by devise.
I've used carrierwave before with no problems but this time, when I go to start the rails server, I get the error:
/var/lib/gems/1.9.1/gems/carrierwave-0.10.0/lib/carrierwave/mount.rb:46:in `uploader_option': undefined method `validate_integrity' for :ImageUploader:Symbol (NoMethodError)
There's very little information about this out there but I did read that you could ignore some errors using :ignore_integrity_errors. After trying a few times (and getting a lot of syntax errors) I finally settled on passing that in like so:
mount_uploader :profile_image, :ImageUploader, :options => {:ignore__integrity_errors => true}
I still think my syntax is incorrect because the NoMethodError persists. Any help at all on this matter would be much appreciated!
EDIT
I reverted to the commit before I started working with carrierwave, started my server, and tried again. In my browser I'm now getting the error:
uninitialized constant CarrierWave
on this line
class ImageUploader < CarrierWave::Uploader::Base
and this error apparently happens when the routes are loaded. I'm really confused about this, I have the gem in my Gemfile and I ran bundle install.
There are a couple of things going on here. First, the reason you're seeing the uninitialized constant CarrierWave error is probably just a consequence of not having restarted the server or something silly like that. If you've properly included it in the Gemfile and bundled, you should be fine.
To your original issue, though, there are three problems. The first is that you're passing a symbol as the uploader, and you need to pass the class. So you should pass ImageUploader, not :ImageUploader. The second issue is that you must pass the options hash directly to the uploader, not inside a parent hash with an options key. The third problem is that you've included an additional underscore in the ignore_integrity_errors symbol. So you should be using this:
mount_uploader :profile_image, :ImageUploader, ignore_integrity_errors: true
It would help to know what version to rails you are using. I've had this issue before but a server restart helped out.
Also, it looks like others had this issue. Maybe try doing what some of the users did here: https://github.com/carrierwaveuploader/carrierwave/issues/399

memcache error illegal character in key (Ruby 1.8.7 / Rails 2.3.9)

I am getting the following error in one of my rails app [Ruby 1.8.7 + Rails 2.3.9]
A ArgumentError occurred in home#dashboard:
illegal character in key "dashboard_prod:views/reports/1050 - 097"
/opt/ruby-enterprise/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/vendor/memcache-client-1.7.4/memcache.rb:643:in `get_server_for_key'
I googled and found that someone had similar problem at: http://www.coffeepowered.net/page/2/
on that page it is mentioned that, this should work:
class ActionController::Caching::Actions::ActionCachePath
def path
#cached_path ||= Digest::SHA1.hexdigest(#path)
end
end
But I am not sure where should I type this. So I have two questions:
How to solve problem at hand
Where should I write the code like the above where we are overriding some standard class or class defined in a Gem.
Any help would be appreciated.
I think the post you found is suggesting you create a monkey patch with that code. Create a file under Rails.root + 'lib/' with those contents, and make sure it loads after ActionController (which should be the default). The patch will override ActionController's default code.
You definitely want something like that--I always ensure my memcached keys are hashed. It makes them a little more difficult to debug, but it protects against problems like this and also key-length overflow errors when someone creates a key that's too long for memcached.

Ruby on Rails model inside namespace can't be found in controller

I'm new to rails and can't figure out this issue...
I have a controller
Admin::Blog::EntriesController
defined in app/controllers/admin/blog/entries_controller.rb
And I have a model called
Blog::Entry
defined in app/model/blog/entry.rb
When I try to access my model from the controller, I get a "uninitialized constant Admin::Blog::EntriesController::Blog" from this line:
#blog_entries = Blog::Entry.find(:all)
Clearly it is not finding the namespace correctly which is odd because according to what I have read, I have placed my model in the correct folder with the correct syntax.
Any ideas on how I can fix this?
Thanks
Try:
#blog_entries = ::Blog::Entry.find(:all)
It's currently looking for the wrong class. Using :: before Blog will force it to look from the top level.
It is now 2011 and we are in Rails 3.1 territory, but this issue still arises. I just ran into it with a namespaced controller referencing a non-namespaced model, but only when there were no rows for that model in the database!
Prefixing the model name with :: fixes the problem.
You can achieve a custom table name by using
set_table_name('foo')
at the top of your model.
As for multiple namespaces, you might be able to get away with using
polymorphic_path(#the_object)
to generate your urls as it does more basic inference (in my experience at least, maybe form_for uses it under the hood).
Yeah, from looking at the code form_for uses polymorphic_path under the hood.

Resources