Okau, I have a rails app which works fine on my localhost. Now I have a new problem on the logs which seems to have an issue with the syntax. I have javascript include tags as below :
Okay This is the main error
ActionView::Template::Error (/app/app/assets/javascripts/jq_scroll/jq_animate.js has a invalid UTF-8 byte sequence):
How do I resolve this?
Are you interpolating any I18n string into the JS file or does it contain any non-ASCII characters? If so, try adding a "magic comment" at the top of the file (assuming the file has the extension .js.erb):
<% # coding: utf-8 %>
// JS file content...
Related
Please i need an explanation on how ruby codes in javascript files are been executed in rails.
i need to know why the code below would run
var path = '<%= "#{Rails.root}/public/time_table/time_table.json" %>';
and
<% file = File.new("#{Rails.root}/public/time_table/time_table.json",'r') %>
would not.
where and when do JavaScript files get executed in rails.
when the application sees a ruby code syntax in a filename.js.erb file, how those it treat it. Please i really need this explanation.
JavaScript is executed in the browser. js.erb files are templates for the code that will be sent and executed in the browser.
In your first example the ERB template will result in:
var path = '/path/to/root/public/time_table/time_table.json';
This JavaScript will be sent and run by the browser.
In your second example you won't insert any text into the output (you used <% instead of <%=). The code between <% and %> is Ruby. It opens the file for reading and assigns to file. It doesn't read the file or otherwise insert anything into the output.
In general, ERB is used to generate content that will be sent to the browser. html.erb is for HTML code. js.erb is for JavaScript code. The templates are expanded on the server and the resulting output is sent to the browser.
Added based on comments. To show the content of the file you need to read it. File.new just opens it and allows you to manipulate the file (read, write, truncate, etc.). I recommend you read the file with File.read and insert it into the template with:
<%= raw File.read("#{Rails.root}/public/time_table/time_table.json") %>
You may also consider moving the file to a partial, e.g. app/views/time_tables/_time_table.json.erb and rendering in the controller with:
render partial: 'time_tables/time_table.json.erb'
I have installed EasyPHP 16.1 with Ruby and tested with Ruby files (.rb) and they are working. So Ruby is properly installed in EasyPHP. However the embedded ruby files (.erb) do not work in EasyPHP. For example out put of following erb file is something as given below.
Ruby File (.erb)
<h1>This is a test</h1>
<%= puts "Content-type: text/html"%>
<%= puts "Ruby example"%>
<% puts "#{10*5}"%>
%>
Result (as displayed in browser)
Click here to see what gets displayed in browser
That's not how it works.
Erb is a templating language and you'll need Ruby code to load the template, compile it and render it to the user. You cannot just server an .erb file and expect it to work.
I am runnning Ruby on Rails 4.1 in production mode and I send a successful email from my web application to my GMail account. This email contains an image (the app logo) but this image is not displayed in the message body.
Logging the image request on the server side I get the following error:
ActionController::RoutingError (No route matches [GET] "/assets/my_app_logo.png")
Inspecting the email source code I get:
<img class="CToWUd" alt="MyAppName" src="https://ci4.googleusercontent.com/proxy/J8xdV03MSjJGOJh6F8T5ntqhjC2YJAiShAWJshvcOLn9THWAC5hKwp4DCLc6csuoojWlaKzPXjt-6zBAkZZvzpYMH=s0-d-e1-ft#http://www.my_app_name.com/assets/my_app_logo.png">
What is the problem? How can I solve it?
In my partial template I have:
<%= link_to(image_tag("#{root_url}assets/my_app_logo.png", :alt => MyAppName), root_url.to_s) %>
Update after #kasperite's comment
Can you post what's in application.css? also is it application.css
under assets/stylesheets?
Yes, my application.css.scss file is assets/stylesheets:
// ...
.my_app_logo { background-image: image-url("my_app_logo.png"); display: inline-block; }
// ...
However the application.css.scss is not used for rendering the email. I use the code as stated in the above partial template.
I think the problem is that after your assets get compiled for Production, my_app_logo.png does not technically exist anymore. It is actually renamed by appending a fingerprint onto the asset, so the actual asset is my_app_logo-SOME_FINGERPRINT_STRING.png.
The crux of the issue is you are attempting to do an absolute path to the image in your image_tag helper. Try doing this instead:
<%= link_to(image_tag("my_app_logo.png", :alt => MyAppName), root_url.to_s) %>
This should generate the proper link to the renamed image.
I have a simple CoffeScript file in the /assets/javascript pipeline that is working fine when adding CoffeScript to it.
If I add a simple <%= puts "hello world" %> on top of it I expect it to render "Hello World" on the file, yet I get this error message
throw Error("ExecJS::ProgramError: Error: Parse error on line 1: Unexpected 'COMPARE'\n (in /Users/user/Sites/app/app/assets/javascripts/application/application.js.coffee)")
Any idea what is preventing me to render Ruby and how to solve it?
You should have a file extension .erb before the .coffee extension. application.js.erb.coffee
did you try adding .erb to the file name to enable ruby preprocessing?
I am using Ruby on Rails 3.1.0 and I would like to know how to correctly state internationalization keys/values in YAML files (I have a couple of questions/doubts...). That is, I have a locale file containing the following code:
en:
# Note the 'style' HTML property and the ':' at the end
test_key_html: <span style='color: #4682B4;'>Test text</span>:
How should I correctly add colon (punctuation) to a YAML file (maybe by using HTML code)?
How should I properly state the HTML 'style' property in the YAML file? What do you advise about?
Those translation files aren't meant to have HTML. I would avoid having the entire HTML string in there, instead just have the string "Test text" and move the html portion back into your template or helper.