I have a Rails 3 app where I am trying to populate a javascript variable with every Nation in my database (less than 300 nations) as a JSON object. This is the relevant line in my nations.js.erb file:
_this.nations = <%= Nation.all.to_json :only => [:id], :methods => :text %>;
When I call my js file in a browser, /assets/users.js which does a require of the nations file, the _this.nations variable is populated perfectly. When I try to do a precompile I get the following:
$> rake assets:precompile
$> rake aborted!
uninitialized constant Nation (in nations.js.erb)
So my question is this: is it possible to reference the Nation model, or any model, from within the js.erb file for precompiling? I also tried using my NationsHelper but my error just changed to uninitialized constant NationsHelper.
I'm fairly new to RoR so if relevant information is needed that I haven't provided, please just ask.
If you have config.assets.initialize_on_precompile set to false somewhere then try enabling it
config.assets.initialize_on_precompile = true
Why don't you make the call in the controller
controller
#nations = Nation.all
nations.js.erb
_this.nations = <%= #nations.all.to_json :only => [:id], :methods => :text %>;
Related
I'm following this gist: https://gist.github.com/riyad/1933884/#file-bootstrap_breadcrumbs_builder-rb-L2
I have the following in my view:
<%= render_breadcrumbs :builder => ::BootstrapBreadcrumbsBuilder, :separator => "ยป" %>
I put this in my config/application.rb: config.autoload_paths += Dir["#{config.root}/lib/"]
I put the bootstrap_breadcrumbs_builder.rb in my config/lib.
I get this error though: uninitialized constant BootstrapBreadcrumbsBuilder
Since this needs to be auto loaded why not make it an initializer rather than put it in autoload_paths? (i.e. config/initializers/bootstrap_breadcrumbs_builder.rb)
Also if you would like to use autoload_paths it should not be in config/lib but just in lib. config.root will return the base path for the application e.g. /path/to/your/application and lib would be be a sub directory of this.
It is essentially the same as Rails.root so I think this is your issue (you just placed the file in the wrong spot.)
Run a console and type Rails.application.config.root and you will see what I mean.
I have some named routes like this rake routes:
birthdays GET /birthdays(.:format) birthdays#index
In a rakefile I simply want to be able to call birthdays_url like I would in my view.
task :import_birthdays => :environment do
url = birthdays_url
end
But I'm getting an error undefined local variable or method 'birthdays_url' for main:Object
You can either use this example code in your rake task:
include Rails.application.routes.url_helpers
puts birthdays_url(:host => 'example.com')
or you can use this example code in your rake task:
puts Rails.application.routes.url_helpers.birthdays_url(:host => 'example.com')
If you only want the path part of the URL, you can use (:only_path => true) instead of (:host => 'example.com'). So, that would give you just /birthdays instead of http://example.com/birthdays.
You need either the (:host => 'example.com') or (:only_path => true) piece, because the rake task doesn't know that bit of information and will give this error without it:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
for Rails 4 include code with your domain at the top of your rake task
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'
use this:
Rails.application.routes.url_helpers.birthdays_url
or to be less verbose:
include Rails.application.routes.url_helpers
url = birthdays_url
I have a custom class stored in /lib (/lib/buffer_app.rb):
require 'HTTParty'
class BufferApp
include HTTParty
base_uri 'https://api.bufferapp.com/1'
def initialize(token, id)
#token = token
#id = id
end
def create(text)
message_hash = {"text" => text, "profile_ids[]" => #id, "access_token" => #token}
response = BufferApp.post('/updates/create.json', :body => {"text" => text, "profile_ids[]" => #id, "access_token" => #token})
end
end
I'm attempting to use this this class in an Active Admin resource and get the following error when in production (Heroku):
NameError (uninitialized constant Admin::EventsController::BufferApp):
It's worth noting I have this line in my application.rb and that this functionality works locally in development:
config.autoload_paths += %W(#{Rails.root}/lib)
If I try include BufferApp or require 'BufferApp' that line itself causes an error. Am I having a namespace issue? Does this need to be a module? Or is it a simple configuration oversight?
I had exactly the same problem with Rails 5 alpha. To solve it, I had to manually require the file:
require 'buffer_app'
and not: (require 'BufferApp')
Even if Michal Szyndel's answer makes great sense to me, after manually requiring the file, prefixing :: to the constant was non influential in my case.
Anyway I am not satisfied with the manual requiring solution because I need to add code which is environment specific. Why don't I need to require the files manually in development?
Change this
config.autoload_paths += %W(#{Rails.root}/lib)
to this
config.eager_load_paths += %W(#{Rails.root}/lib)
eager_load_paths will get eagerly loaded in production and on-demand in development. Doing it this way, you don't need to require every file explicitly.
See more info on this answer.
Error line says it all, you should reference class as ::BufferApp
I am using Rails, Dalli gem, friendly_id and Memcachier on Heroku.
My problem is similar to an issue I have had previously but that stopped working after I started using Memcache instead of the default Rails cache. It should be noted that I am not very familiar with Rails caching and it is quite likely that I do many things wrong (or fail to consider simple things).
production.rb
config.action_controller.perform_caching = true
config.cache_store = :dalli_store, 'mc2.ec2.memcachier.com:11211', { :namespace => 'my_app_name', :expires_in => 40.days, :compress => true, :username => 'asdf', :password => 'asdf' }
Gift#show - controller
unless fragment_exist?("gift-show--" + #gift.slug)
# Perform slow database fetches etc
end
Gift#show - view
<% cache('gift-show--' + #gift.slug) do %>
# Create HTML with lots of images and such
<% end %>
This worked fine before I started using Memcachier on Heroku. My guess is that fragment_exist? does not check in Memcachier but rather in "default Rails cache" (if there is a difference). I have tried to use Rails.cache.exist?("gift-show--" + #gift.slug) instead of fragment_exist? but it doesn't work.
I have loaded the particular gift#show-view a few times to make sure it is cached. In the logs I can also see Read fragment views/gift-show--cash-stash (1.3ms) (after the controller) which I believe is a proof for that a fragment actually exist. It is just that it is going through the slow (4 seconds) gift#show-controller when it is not necessary.
If I enter the console on Heroku and type "Rails.cache.read('gift-show--cash-stash')" I get a nil response.
Another peculiar things is that if do the following in the console:
irb(main):014:0> Rails.cache.write("foo", "bar")
=> true
irb(main):015:0> Rails.cache.read("foo")
=> nil
That is odd, isn't it?
So, what should I use, instead of fragment_exist? in order to make this work?
I am not 100% sure this is the solution but I added the 'memcachier' gem (which I didn't have) and altered my production.rb to:
config.cache_store = :dalli_store
This actually also solved another, completely different issue, to my great surprise!
In my routes.rb I have a custom path defined:
match "foo/copy" => "foo#copy", :via => [ :post ], :as => "copy_foo"
I have an initializer in my config/initializers directory named https_by_default.rb, which contains one line:
Rails.application.routes.default_url_options[:protocol] = 'https'
If I run rails console and type Rails.application.routes.default_url_options, it returns {:protocol => 'https} as expected.
But in my view where I am using the generated custom path _url helper, it doesn't generate the URL with https. <%= copy_foo_url() %> in the ERB returns http://localhost:3000/foo/copy.
What am I missing in order to get my named routes helpers to respect default_url_options?
You should use the following instead:
<%= copy_foo_path() %>
And in your application config file add.
config.force_ssl = true
This enables ssl on all your Rails environments. If you want to enable ssl on your production environment only, then just add the line to your production config file.