I have a yml file and i need the keys to be used in the application.js file in the scripts. I cannot able to access it from the script. Please help me. I have the following lines in the initializer
APP_CONFIG = YAML.load_file("#{Rails.root}/config/filename.yml")[Rails.env]
and i have the following keys in my yml file
development:
key1: 782364764527225794828437
key2: sdjfbjs7e834284383984729
key3: 73465365egfrf36462874727
and i access the keys in application.js file as
APP_CONFIG['key1']
but it seems to take nothing. But when i print the same thing in the view body as
<%= APP_CONFIG['key1'] %>
then it returns the value of the key1.
What should i do to access the value in application.js file. It also not works in the script body in the view itself.
I have a yml file and i need the keys to be used in the application.js file in the scripts. I cannot able to access it from the script. Please help me. I have the following lines in the initializer
APP_CONFIG = YAML.load_file("#{Rails.root}/config/filename.yml")[Rails.env]
and i have the following keys in my yml file
development:
key1: 782364764527225794828437
key2: sdjfbjs7e834284383984729
key3: 73465365egfrf36462874727
and i access the keys in application.js file as
APP_CONFIG['key1']
but it seems to take nothing. But when i print the same thing in the view body as
<%= APP_CONFIG['key1'] %>
then it returns the value of the key1.
What should i do to access the value in application.js file. It also not works in the script body in the view itself.
Share Improve this question asked Dec 30, 2013 at 6:42 logeshlogesh 2,6724 gold badges37 silver badges61 bronze badges3 Answers
Reset to default 4You cannot execute ruby mand in a Javascript file
You need to figure out some other way to load the values in application.js
Like initialize them in app/views/layouts/application.html.erb
as a global variable
<script>
var key = '<%= APP_CONFIG['key1'] %>';
</script>
and use it in any Javscript file
window.onload=function(){ alert(key);};
You can not directly access your yml file content from JS file. What you can do is on your view page assign these keys to a js variable then it will be available to you for JS work
<script>
var appConfigKeys = “<%= j APP_CONFIG.to_json.html_safe %>”;
console.log(appConfigKeys);
</script>
If you want it to be available in other JS file as then you need to take help of global varibales.
<script>
appConfigKeys = “<%= j APP_CONFIG.to_json.html_safe %>”;
console.log(appConfigKeys);
</script>
Above solution will give you all the keys as a javascript hash If you need only first key, you can assign single key to a JS variable.
<script>
appConfigKey = “<%= APP_CONFIG['key1'] %>”;
console.log(appConfigKey);
</script>
I came to this question since I needed access to my Rails locale YAML from JS, more specifically the months array stored in there. My solution has been to create some HTML with the values I need with ERB and set display: none
on the parent div (done here using bootstrap's d-none
class).
In the locale YAML:
en:
date:
month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
In the view:
<ul class="d-none" id="months">
<%= I18n.t('date.month_names').each do |month_name| %>
<% if month_name.present? %>
<li><%= month_name %></li>
<% end %>
<% end %>
</ul>
In the JS file:
const months = document.getElementById('months');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745394966a4625836.html
评论列表(0条)