ERB files not working in EasyPHP - ruby-on-rails

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.

Related

understanding where and when rails code runs( js.erb)

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'

Rails video and image tag not working in activeadmin

Hi I'm very new to rails and came up with this issue. I need your help. Please.
Activeadmin parses the code as raw text so i used<%= raw(#classroom.body) %> in my show.erb to unescape the html tags. but i figured this is the reason why it escapes the ruby video_tag.
I checked out the activeadmin arbre but didn't understand it. i also tried using html video tags but for some reason it shows the video template but doesn't connect to the video file in app/assets/videos.
But when I parse the HTML code directly in the show.erb, the video works perfectly.
The video file can be found in app/assets/videos/Heather.mp4
<%= video_tag "Heather.mp4", :controls => true %>
The first picture is how it looks like in the web browser
The second is the html code in active admin
The third is my show.erb file
The raw method produces raw HTML, not raw ERB. You won't be able to parse your <%= %> tags from there.
Reference the Rails Docs for raw

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

Rails 3.1 - embedding flash.swf files - best practices?

Having problems embedding my .swf file in rails 3.1. In past versions of rails I'd use swfobject and simply do:
<div id="swfbox">you don't have flash</div>
<script type ="text/javascript">
swfobject.embedSWF("swf/AudioRecorder.swf", "swfbox", "400", "400", "10.0.0", "");
</script>
This isn't working in rails 3.1. I'm starting to understand the asset pipeline but I'm still confused where to put the .swf file. So far I've tried of putting everything in /public then /app/assets with a combination using:
<%= asset_path("swf/AudioRecorder.swf") %>
You should be able to put it in any directory you like under app/assets, then reference it with
<%= asset_path("AudioRecorder.swf") %>

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