I used to use rails-sprockets 2.x and was using the following in an email layout template to include the css
Rails.application.assets.find_asset('file').to_s.html_safe
However, this no longer works in rails-sprockets 3.x and it is suggested to use
Rails.application.assets_manifest.assets['file.css']
This only returns the string name of the file (if it exists). How would I get the body of the file so I can output it in the view?
I just ran into this issue too, although I upgraded to sprockets 3.x about 7 months.
I threw this together as quick as I could (there is likely a better solution out there...) - this will get you the path name - just use File.read()
def find_asset_path(asset_name)
if Rails.application.assets
Rails.application.assets.find_asset(asset_name).pathname
else
name = Rails.application.assets_manifest.assets[asset_name]
File.join(Rails.public_path, 'assets', name)
end
end
I use carrierwave 0.9.0 with Rails 4 and I'm trying to make a custom error message. After doing some search, I found this answer:
en:
errors:
messages:
extension_white_list_error: 'My Custom Message'
I tried this by going to config/locales/en.yml then adding the code above, but nothing changed. The error message is still the same.
There is also the same issue on github here, but no answers, I think this problem is specific just with Rails 4 but not sure, have you the same problem when you use Rails 4?
There seem be some loading issue with Rails 4.0 with regards to i18n files
Ideally what should be I18n load paths as per 3+
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activesupport-4.0.0/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activemodel-4.0.0/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activerecord-4.0.0/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/actionpack-4.0.0/lib/action_view/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/locale/en.yml
/Users/joshianiket22/carrierwave_tester/config/locales/en.yml
What is seen in Rails 4.0
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activesupport-3.2.11/lib/active_support/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activemodel-3.2.11/lib/active_model/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/activerecord-3.2.11/lib/active_record/locale/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/actionpack-3.2.11/lib/action_view/locale/en.yml
/Users/joshianiket22/workspace/zenjavi/carrierwave_tester/config/locales/en.yml
/Users/joshianiket22/.rvm/gems/ruby-1.9.3-p327#rails3_2/gems/carrierwave-0.9.0/lib/carrierwave/validations/../locale/en.yml
One can clear see the difference between the two the carrierwave en.yml is loaded after a the application specific en.yml and there is your issue
I suggest there is no easy way unless you the change the load_paths in rails application and some how manage to change the order of load_paths of as expected
I have given a pull request over here. Completely at awe of Carrierwave guys to decide on it
Hacky Solution :
I was refraining in giving you this solution earlier but still if you want it that bad here what you can do
define a file in lib directory(let say auto_load_i18n.rb) and assign the lib path to autoload (in application.rb)
config.autoload_paths += %w(#{config.root}/lib)
Inside auto_load_i18n.rb write this
I18n.load_path.delete(Rails.root.join("config/locales/en.yml").to_s)
I18n.load_path << Rails.root.join("config/locales/en.yml").to_s
And require the lib file at the top of your application_controller.rb
require 'auto_load_i18n'
class ApplicationController < ActionController::Base
and I guess everything would work then
You can now understand as to why I was refraining in giving this as a possible solution :)
Hope this help
It's fixed now guys:
https://github.com/carrierwaveuploader/carrierwave/pull/1264
Thanks for the patience.
I use a rails 5.1
I created a file config/locales/carrierwave.ar.yml and wrote
ar:
carrierwave:
errors:
messages:
min_size_error: "حجم الصورة لابد أن يكون أكبر من %{min_size}"
max_size_error: "حجم الصورة لابد أن يكون أقل من %{max_size}"
....
You can take a look at this file :)
Hi I am trying to setup Vanity gem into a rail 3 application.
I am creating a custom metric to experiment a bit.
So I created it following the suggestions on their website:
metric "Signups" do
description "Signups completed"
def values(from, to)
(from..to).map { |i| 24 }
end
end
The file is located at the right place and is loaded, vanity picks it up but somehow it looks like there is an internal error in the way vanity works.
Of course: those values are only there for a testing purpose, they will be replaced by real ones later.
I get the following error when running this experiment
Thank you!
There seems to be an error in the gem:
in this file
.rvm/gems/ruby-1.9.2-p290/gems/vanity-1.7.1/lib/vanity/templates/_metric.erb
where we can read the following line
js = data.map { |date,value| "['#{**date.to_time.httpdate**}',#{value}]" }.join(",")
vanity_html_safe(%{<div class="chart"></div>
if we change date.to_time.httpdate to date.to_s.to_time.httpdate it works.
I have a rails app in a subdirectory of my server, something like www.domain.com/sub
I need to send an url by e-mail, so I tried to use "resource_url" but it generates a link like www.domain.com/resource_path, where should be wwww.domain.com/sub/resource_path.
How can I solve this ?
Thanks!
In Rails 2.3.8 you can add a line like this to your config/environments/production.rb
ActionController::Base.relative_url_root = "/sub"
I am not sure what the equivalent is for Rails 3, but see this question if that is what you are using:
What is the replacement for ActionController::Base.relative_url_root?
I'm messing around with rails 2.3 templates and want to be able to use the app name as a variable inside my template, so when I use...
rails appname -m path/to/template.rb
...I want to be able to access appname inside template.rb. Anyone know how to do this?
Thanks
I was looking for an answer to this question. unfortunately the answer above (#root) doesn't seem to work in Rails 3.
Here's the variables you can access in Rails 3 app templates (even easier):
#app_name
#app_path
Thanks for the answers. Mike Woodhouse, you were so close. Turns out, all you need to do to access the appname from inside your rails template is...
#root.split('/').last
The #root variable is the first thing created when initializing templates and is available inside your rails templates. RAILS_ROOT does not work.
In Rails 3, use the app_name attribute.
See the documentation for the Rails::Generators::AppGenerator.
I ran into a similar problem, none of the variables listed above were available to me in Rails 4. I found that #name was available while running
rails plugin new engines/dummy -m my_template.rb
There are other useful variables available from within the template. You can see for yourself and play around by utilizing pry. Inside my template I added
require 'pry'; binding.pry
and then ran ls to show a list of available instance variables
ls -i
instance variables:
#_initializer #app_path #behavior #destination_stack #extra_entries #name #output_buffer #shell
#_invocations #args #builder #dummy_path #gem_filter #options #rails_template #source_paths
#after_bundle_callbacks #author #camelized #email #in_group #original_name #shebang
There's probably a more straightforward way, but this seems to work:
RAILS_ROOT.split('/').last
EDIT: Bleah - this got voted down once, and the voter was right. If I'd read the question more carefully, I'd have noticed the 2.3 and template.rb elements. Apologies.
I suspect that RAILS_ROOT won't have been created at the point that you need the app name. Looking at ruby\lib\ruby\gems\1.8\gems\rails-2.2.2\bin\rails, however, almost the first thing that happens is this:
app_path = ARGV.first
It's used at the end of the script to allow a chdir and freeze to be done if needed - I didn't know I could insta-freeze at creation, so I learned something new at least. ARGV then gets used here:
Rails::Generator::Scripts::Generate.new.run(ARGV, :generator => 'app')
which quickly gets us to the place where ARGV is really handled:
rails-2.3.1\lib\rails_generator\scripts.rb
where I see
Rails::Generator::Base.instance(options[:generator], args, options).command(options[:command]).invoke!
Somewhere below here is probably where the templating gets handled. I'm afraid I'm at a very early stage with 2.3 and templating is an area that I haven't looked at yet.
Does that help any better than my first effort?
RAILS_ROOT will give you the absolute path to your root directory. Your app name will be the portion of the string after the final '/' which you can grab in any number of ways.
EDIT: Not quite enough to get the job done. Mike and Dan iron it out below.
I believe the preferred way now is to call Rails.root and no longer RAILS_ROOT. Apparently someone on planet rails has an aversion to uppercase or some similar important reason. As of 2.3.5 they both appear to work.
I was getting error
`template': undefined local variable or method `app_name'
ruby 1.9.2p290, rails 3.2.11, thor 0.18.0, Windows
but with rails 2.3 generator:
class DynanavGenerator < Rails::Generators::Base
(can't be sure whether this error happened under rails 3.0.9 or earlier)
changed class definition to be:
class DynanavGenerator < Rails::Generators::NamedBase
which then gave:
No value provided for required arguments 'name'
I then added a 'name' ("something" below):
rails generate dynanav something --force
which gave the original error, so I then added:
def app_name
#name.titleize
end
to the class and all was well.
As of Rails 4 (maybe earlier versions?), use Rails.application.class to get the application name. For example, if your app is named Fizzbuzz, here are a few ways you might access it:
rails(development)> Rails.application.class
=> Fizzbuzz::Application
rails(development)> Rails.application.class.name
=> "Fizzbuzz::Application"
rails(development)> Rails.application.class.parent
=> Fizzbuzz
rails(development)> Rails.application.class.parent.to_s
=> "Fizzbuzz"