Can't render ruby code inside CoffeScript - ruby-on-rails

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?

Related

ERB files not working in EasyPHP

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.

How to add html-file with JavaScript code in template Slim?

There are HTML-file that contains JavaScript code. This JavaScript code loads an image and positioning it in a certain place. If failed, a message displays.
I need to include this file in template Slim.
I can include one Slim template to another by the following:
=render 'some/path/some_pattern'
How to include HTML- file to my template Slim?
The best way to add some javascript to your slim file is either by including the javascript file using
= javascript_include_tag 'name of the file'
or by directly adding the javascript code to your slim file, using
javascript:
code line 1
code line 2
...

rails : heroku error logs

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...

stuck with haml templates

I want to transform some haml (*.html.haml files) into xhtml. The haml command says "Usage: haml [options] [INPUT] [OUTPUT]". So I tried it with the following response:
Exception on line 1: undefined method `content_for' for #<Object:0xb730af2c>
I noticed that there are different formats which are all called haml. I noticed one which uses angle brackets a lot. Do I need some kind of preprocessing?
Here is a sample html.haml file that I want to transform:
- content_for :head do
= stylesheet_link_tag 'jquery.autocomplete'
= javascript_include_tag 'jquery.autocomplete'
- javascript_behaviour '$("input#user_full_name").autocomplete("project_roles/auto_complete_for_user_full_name")'
Note: I know how to google so I am looking for specific advice.
You are using a rails helper, content_for, so when you invoke haml using the command line program it doesn't know what does it stands for.
So either add the haml gem to your gemfile and save the template in a .html.haml template and try to render it or replace the content_for with the html result generated by rails.
What you wanted is to convert Haml to Erb - the Ruby templating language which looks like HTML. Haml doesn't do that, so it tried to render your haml template into xhtml instead of converting it to another template.
You could have used the haml2erb gem to do the conversion. It would go like this:
Dir["**/*.haml"].each do |filename|
File.open(filename.gsub(/haml$/, 'erb'),'w') do |f|
f.puts Haml2Erb.convert( File.read(filename) )
end
end

How to render an xml file in rails

I am building a rails app to display test results for an application that is already written.
The application generates an xml file. I would like to have rails read the xml file, it's associated xsl file and render an html partial.
I'm able to get it to render it as a file using <% render :file => 'file_path_not_in_views/file.xml' %> and the contents of the text of the xml file is displayed, but none of the styling is applied.
Thank you.
a

Resources