Ruby Builder - XML output is encoding HTML entities - html

I have a small ruby script which uses Builder.
require 'rubygems'
require 'builder'
content = <<eos
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
eos
xml = Builder::XmlMarkup.new
xml.instruct! :xml, :version => '1.0'
xml.book :id => 1.0 do
xml.keyPic "keyPic1.jpg"
xml.parts do
xml.part :partId => "1", :name => "name" do
xml.chapter :title => "title", :subtitle => "subtitle" do
xml.text content
end
end
end
end
p xml
When running from the CLI (Cygwin), I get the following:
<?xml version="1.0" encoding="UTF-8"?>
<book id="1.0">
<keyPic>keyPic1.jpg</keyPic>
<parts>
<part partId="1" name="name">
<chapter title="title" subtitle="subtitle">
<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em>
</text>
</chapter>
</part>
</parts>
</book><inspect/>
However, the output I would like between is:
<text>
SOME TEXT, GOES TO UPPERCASE
other text
<em>italics<em/>
</text>
I have tried using the htmlentities gem 'decoding' the content but to no avail.

Use the << operation to insert your text without modification.
xml.text do |t|
t << content
end

Related

For loops in ejs

I have a array containing some html content like this.
const articleArray=['<p>first text</p>\r\n','<p>second text></p>\r\n','<p>third text</p>\r\n']
I need to render this in an ejs file through a get request
request.render('viewArticles',{array : articleArray})
where 'viewArticles' is an ejs file. I have used the middleware to set the view engine as ejs.
This is the code in ejs
<% for(arr in array) {%>
arr
<% } %>
I am not able to render any html. How to solve this?
'<%- %>' this is help to print html code as it is.
<% 'Scriptlet' tag, for control-flow, no output
<%_ ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it
<%= Outputs the value into the template (HTML escaped)
<%- Outputs the unescaped value into the template
<%# Comment tag, no execution, no output
<%% Outputs a literal '<%'
%> Plain ending tag
-%> Trim-mode ('newline slurp') tag, trims following newline
_%> ‘Whitespace Slurping’ ending tag, removes all whitespace after it
for refrence please click visit this site EJS
<% array.forEach(function(item) {%>
<%-item%>
<% }) %>
Try this
<% array.forEach(function(item,index){ %>
<%= item %>
<% }) %>
I'm not really sure how to do what you want exactly , but take a look at this:
First, instead of you creating an array of HTML elements, how about you create an array of objects, like so :
const articles = [{text: "first text"} , {text: "second text"}, {text: "third text"}];
this way you just have to write the HTML once, and I am fairly new to programming but I don't see a scenario where you would get a response in HTML, usually you query a database for data or from a JSON file... and assuming the array is actually getting passed to the .ejs file, lets iterate though it,
<% for(let a of articles){ %>
<p> <%= a.text %> </p>
</br>
<% } %>
If the array is NOT getting passed, check for these in your code :
// If you installed
..."dependencies" : {
"ejs": "x.x.x",
} //Check the package.json file
// If you imported correctly
import ejs = require("ejs");
// If you specified the views path
app.set("views", path.join(__dirname, "views"));
// And the views engine
app.set("view engine", "ejs");
// And your route
request.render('viewArticles',{ articles : articles }) // Check if the file name is correct, I get this wrong many times

only <a> tag execute from html content in rails

My problem is only execute tag from html content in rails.
I'm using raw, sanitize but all html tag executed
Example
#input = 'go <b>bold</b> <i>bat</i>'
<%=raw #input%>
<%=sanitize #input%>
there are same output: go bold bat
my propose is output: go <b>bold</b> <i>bat</i>
I implementing hash tag like facebook, but user input not safe many hash tag and many html tags
any idea?
thank you
I also struggled with this and the following will help:
#input = 'go'
#input += '<b>bold</b> <i>bat</i>'.encode {xml: :text}
This will format the HTML special characters as raw symbols. More at the docs for String#encode.
You can do:
<% #input = 'go'.html_safe+'<b>bold</b> <i>bat</i>' %>
<%= raw #input %>
#input = 'go'.html_safe
#input += ' <b>bold</b> <i>bat</i>'
<%= #input %>

Why won't my HTML template display images/external css for definitions in my controller in Ruby on Rails?

Problem after problem haha, but I'm so close to finishing my project!!
So I have my Ruby on Rails program done and working. Now i'm trying to integrate it into an HTML template..
I finally got the graphics and template to load properly, but only for the index.html.erb page since that's the starting page. Now my problem is the page defined as "result" from my controller file, won't display properly. It looks like it may be an easy fix, but not sure how. It looks like the inline css styles are working properly based on the format of result, but the images and styling in my style.css aren't loading for the template now (other than for index).
I put the images directory as a sub directory of public. I have style.css in public as well.
I have a page in views called "customerdisplay.html.erb," which contains the HTML coding for my template. One of the div tags in that file is a custom div in my external stylesheet, but when I use that tag, I also set the align to center. The centering works, but everything else from the external stylesheet isn't working.
I'll add the controller code and the view code for reference.
My question:
How can I get the external stylesheet and images to work for "def result" in my controller file?
myRuby_controller.rb
class MyRubyController < ApplicationController
def index
# the first user screen is displayed:
# index.html.erb in views/myRuby directory
# This controller is executed
end
def result
# when the user presses submit, result is called
#data = params[:data] # The data text field
#field = params[:field] # the request
# check for nondigits in the data
if #data =~ /\D/
# if a nondigit, invalid.html.erb is displayed
render :action => 'invalid'
else # valid data
case #field # check the request
when "cu" then # customer data request
# Verify that customer is in the customer table
if Customer.exists?(#data)
# exists, display custdisply.html.erb
render :action => 'custdisplay'
else
# does not, display notfound.html.erb
render :action => 'notfound'
end
when "sr" then # sales rep request
if Customer.exists?(#data)
# exists, display salesrepdisplay.html.erb
render :action => 'salesrepdisplay'
else
# does not, display notfound.html.erb
render :action => 'notfound'
end
when "or" then #orders request
if Customer.exists?(#data)
# exists, display orderdisplay.html.erb
render :action => 'orderdisplay'
else
# does not, display notfound.html.erb
render :action => 'notfound'
end
when "p" then #parts request
if Customer.exists?(#data)
# exists, display partsdisplay.html.erb
render :action => 'partsdisplay'
else
# does not, display notfound.html.erb
render :action => 'notfound'
end
end # end case
end # end if valid data
end
def custdisplay
end
def salesrepdisplay
end
def orderdisplay
end
def partsdisplay
end
def notfound
end
def invalid
end
end
views/customerdisplay.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<link rel="stylesheet" type="text/css" href="style.css">
<title>The Justin Geis Data Access Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="logo"><img src="images/logo.png"></div>
<div id="container">
<div id="header">
<!--blank.. could add stuff if wanted.. -->
</div>
<div id="content" align="center">
<h1>Spartan Hardware Data Access</h1>
<% # get the customer data from the customer table
#custdata = Customer.find(#data)
# copy each field into a variable for display
#id = #custdata.id
#ln = #custdata.last_name
#fn = #custdata.first_name
#bl = #custdata.balance;
#bl = sprintf("%7.2f",#bl) # format as currency
#cl = #custdata.credit_limit
#cl = sprintf("%7.2f",#cl) # format as currency
#sr = #custdata.sales_rep
%>
<h1>customer data for customer: <%=#data %></h1>
<table border=1>
<tr>
<td>ID</td>
<td>Last name</td>
<td>First name</td>
<td>Balance</td>
<td>Credit limit</td>
<td>Sales rep</td>
</tr>
<tr>
<td><%=#id%></td>
<td><%=#ln%></td>
<td><%=#fn%></td>
<td><%=#bl%></td>
<td><%=#cl%></td>
<td><%=#sr%></td>
</tr>
</table>
</div>
<div id="footer"><!--nothing right now --></div>
</div>
</body>
</html>
Please let me know if you have any questions. Thanks!
Your view is being rendered inside another view, called a layout, usually in a file app/views/layouts/application.html.erb. That's why your <head> won't work, because it's inside a <body>.
2 options:
Do it the Rails way and link your stuff from the layout, or alternatively have a yield in the layout that yields a tag from a content_for inside your salesrepdisplay view. http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for
Instead of render :action => 'salesrepdisplay', use render :action => 'salesrepdisplay', :layout => nil

<text> tag in _LoginPartial.cshtml

The _LoginPartial.cshtml file created by the MVC 4 template contains a <text> tag, like this:
<text>
Hello, #Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { #class = "username", title = "Manage" })!
#using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
#Html.AntiForgeryToken()
Log off
}
</text>
As far as I can tell, <text> is not a recognized tag, so why did the folks who created this code use that tag?
As the commenters to the OP said, <text> is special Razor syntax to denote text. As ScottGu's Blog explains, this syntax can be used in lieu of the #: syntax.

Ruby on rails password field and text field alignment

I am new to ruby on rails platform. I am designing a form with text fields. In the beginning, I created a simple html file with css to align the text fields. Below is my css
#input{
padding-left:50px;
padding-bottom:30px;
}
I created text fields using following code:
<div id = "input"><input type = "text" size = "25" name = "fname" value = "first name"></input></div>
<div id = "input"><input type = "text" size = "25" name = "lname" value = "last name"></input></div>
When I ran the web page, I saw the text field aligned properly the way I wanted inside form. However I tried to convert the code in RoR, I am not able to see padding. Below is my code in RoR
<div class="input"><%= f.text_field :fname, :size => 25, :placeholder => 'first name' %></div>
<div class="input"><%= f.text_field :lname, :size => 25, :placeholder => 'last name' %></div>
Can anyone tell me what might be the issue with my approach?
In the css code #input{..} the hash # means that it is looking for an id. After you have changed the code, you set it to class="input". Therefore the css can't find it by id. Change the css to .input{..}.