How to render an xml file in rails - ruby-on-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

Related

How do I render an SVG inline in Rails?

I have an SVG file that's part of a template located at:
vendor/theme/assets/icons/icon-1.svg
How do I render that inline in my view? render partial: path fails and says it can't find a partial.
In your view insert the following:
<%= render inline: Rails.root.join('vendor/theme/assets/icons/icon-1.svg').read %>
If you're going to be doing this multiple times, you may want to refactor this into a helper.
Also, think about the following:
Are you okay with dumping third party code directly into your view?
Is the vendor SVG file updated automatically without review?
Are you sure the vendor SVG file will never contain malicious code?

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

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 save a full html page in Rails

I'm trying to save a webpage from my rails 4 application to disk, using
send_data(render_to_string, :filename => "foo.html").
The file is saved alright, but the css is missing.
I tried adding the type attribute, like so:
send_data(render_to_string, :filename => "foo.html", :type => "text/html")
but it didn't help.
How can I save the file with all the css (and other assets potentially), so that if I click on the saved file I'll see the same thing that I attempted to save?
render_to_string renders only the html part. That returns exact the same string like the browser receives when he loads a html page. Stylesheets and other assets will be loaded in additional requests. Therefore I only see one possible way: render_to_string a html layout with all assets inlined.
An other option might be to open the save as dialog with Javascript. But there seems no standardized way to do so.

Resources