Ruby .to_sentence - ruby-on-rails

In the .to_sentence method, How can I get the last_word_connector to output an HTML string?
For example:
locations.to_sentence last_word_connector: '<br />'
I want the <br /> to actually be interpreted. I have tried adding .html_safe on the end but to no avail.

You'll need to tell the view that the string is "safe" to insert (i.e. it should be inserted as-is - not escaped):
<%= locations.to_sentence(last_word_connector: '<br />').html_safe %>
The html_safe method returns a string that's marked as, well, html-safe. So it won't be escaped.
Use with caution. The reason Rails escapes string by default is that it makes html- and javascript-injection attempts harmless. So if you're not completely sure of the content of locations, then be careful.

Related

Rails tag syntax with multiple data attribute values quotes excluded

Trying to generate
<img data-interchange="[assets/img/interchange/small.jpg, small], [assets/img/interchange/medium.jpg, medium], [assets/img/interchange/large.jpg, large]">
is challenging.
<%= tag "img", data: { interchange: ["[#{photo.image_url(:base).to_s}, small]", "[#{photo.image_url(:big).to_s}, medium]", "[#{photo.image_url(:desktop).to_s}, large]"] } %>
will generate HTML, but the result is still inoperative
<img data-interchange="["[https://[...]base_20160529_115006.jpg, small]","[https://[...]big_20160529_115006.jpg, medium]","[https://[...]desktop_20160529_115006.jpg, large]"]" />
as " gets generated.
What is the proper syntax to get the proper HTML string
In rails you can use ActiveSupport's to_json to encode things into json.
<%= tag "img", data: { interchange: [[photo.image_url(:base), 'small'], [photo.image_url(:big), 'medium'], [photo.image_url(:desktop), 'large']].to_json } %>
Which produces
<img data-interchange="[[\"assets/images/interchange/base.jpg\",\"small\"],[\"assets/images/interchange/big.jpg\",\"medium\"],[\"assets/images/interchange/desktop.jpg\",\"large\"]]" />
Then on the front-end you can use JSON.parse once you retrieve the value via getAttribute or with jQuery data, if you need to use the data on the front-end.
var text = JSON.parse(element.getAttribute('data-interchange'));

Rails - input default value from record

Perhaps I'm being silly but I've been trying to set a default value for an input with ruby on rails for hours and haven't cracked it.
I'm making a partial which can either allow users to create new records but will also show existing records if they exist. Code as follows
<input type="text" <%= (#prices.empty? || #prices.first.name.length == 0) ? 'placeholder="General admission"' : "value=" + #prices.first.name.to_s %> >
Which works perfectly for any value that exists UNLESS there is a space, for example, if price.name = "general admission" OR if price.name = "" (in which case it prints the placeholder) I get the following produced
<input type="text" admission'="" value="'general" id="event_price_name" name="[event][price][0][name]" aria-label="..." class="form-control">
It seems to get tripped up by the space.
Am I trying to use Rails in a way it wasn't designed to be done in? I'm more used to PHP which may be it!
Thanks
You should use the text_field_tag helper to build the input, this is preferred over building it with partial interpolation. Also, placeholder will automatically be overwritten if there is a value so you don't need to handle that part in the code, that's how it behaves by default on the browser.
<%= text_field_tag :admission, #prices.first.name.to_s, {placeholder: 'General Admission'} %>

Escape ruby code inside HTML string

I have an object in my controller
#data="<meta property=\"og:url\"
content=\"<%=SITE_URL%><%=request.request_uri%>\"
/>".html_safe
When I print this in my view file by <%=#data%> the html that it generates is
<meta property="og:url"
content="<%=SITE_URL%><%=request.request_uri%>"
/>
How do i print out/escape the ruby code withing the string?
You're looking for string interpolation, which means that in the string, it substitutes the value of the variable. Surround the variables with #{} like this:
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation
#data="<meta property=\"og:url\"
content=\"#{SITE_URL} #{request.request_uri}\"
/>".html_safe
Now, when you do <%= #data %>, it should show up normally.
#data="<meta property=\"og:url\"
content=\"<%= #{SITE_URL}%><%= #{request.request_uri} %>\"
/>"
In your views use raw helper method
<%= raw(#data) %>
You can try h helper like this.....
<%=h(#data) %>
also raw can be use
<%=raw(#data) %>

Html Encode on a string with symbols

How do I encode a string with symbols like ', & # #' in ASP.Net MVC?
I have tried to use Html.Encode, but it returns '&#39', how do I return a string as the user input?
Thanks alot.
If you intend to insert the string into the HTML markup then you need to HTML encode it:
<%= Html.Encode("',&##'") %>
or if you are using ASP.NET 4.0:
<%: "',&##'" %>
Doing this will properly encode any characters in the string.

asp.net mvc and valid xhtml?

For some reason an html helper is outputting this html which doesnt validate.
the validator tells me
There is no attribute "Length"
<%= Html.CheckBox("Medicamentos", Model.Medicamentos) %>
is outputting
<input type="checkbox" value="true" name="Medicamentos" id="Medicamentos" checked="checked" length="4">
I assume that it's matching the signature that takes a string and an object since I don't know what Model.Medicamentos is. In that case it takes the properties of the object and turns them into attributes on the element. I suspect that you simply want to use the Checked attribute on the Model property specified as the default value of the checkbox, i.e.,
<%= Html.CheckBox( "Medicamentos", Model.Medicamentos.Checked ) %>
In, which case, assuming that Checked is boolean it will match the correct method signature on the helper extension.

Resources