rails helper - how can I get a helper to give me a `br/` (or other markup)
Use tag(:br)
instead of "<br/>"
.
content_tag(:br)
creates opening and closing br
tags and using raw
or html_safe
is just ugly (not to mention dangerous).
Rails - label form helper insert html tags inside content
The string needs to be marked as HTML safe so that Rails will not sanitize the output. Here is the concise way to write the label.
<%= f.label :foo, 'Foo <br /> Bar: '.html_safe %>
In Rails - is there a rails method to convert newlines to br?
Yes, rails has simple_format
which does exactly what you are looking for, and slightly better since it also adds paragraph tags. See
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
Example:
simple_format(mystring)
Note that simple_format
allows basic HTML tags, but also passes text through sanitize
which removes all scripts, so it should be safe for user input.
Rails -- Add a line break into a text area
The problem isn't so much editing the value as it is rendering it later. To add newline characters to your value while editing it in a textarea, just hit the return key. When you re-edit that value later, the whitespace should still be there.
Rendering the whitespace is the tricky part. In HTML, whitespace is generally insignificant. A renderer like the one your browser uses will display a single space for any continuous string of whitespace. So merely dumping the value onto the page won't be enough:
<%= obj.description %>
Even though your value may be "One \t \n \n Two"
, it will show up on the screen as "One Two"
.
To get those new line characters to actually separate the lines when displayed, you'll need to convert them to HTML before rendering:
<%= obj.description.gsub(/\n/, '<br/>') %>
Of course, if users are entering data that will be included in your HTML, you should be escaping the values to protect against XSS. If new lines are the only thing you need to support, it should be as simple as this:
<%= h(obj.description).gsub(/\n/, '<br/>') %>
If you want to allow more complex formatting, look into Markdown and Textile (both of which Rails provides helper view methods for). Just be sure to investigate what if any support they provide for XSS prevention.
rails activerecord concern, including url helpers
In your htmlizeBody
function:
include Rails.application.routes.url_helpers
include ActionView::Helpers
This is in the wrong scope, moving it underneath extend ActiveSupport::Concern
will resolve your error.
Another question you could ask yourself is why you need to use view helpers at the concern level?
include Rails.application.routes.url_helpers
is usually used when modifying the default url host options (typically when you need to interface with an external API). In this case, it would make sense to use it in the /lib
directory.
See this SO post and this post on using url helpers for more info
Adding a new line/break tag in XML
The solution to this question is:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake. <br />
Danish topping sugar plum tart bonbon caramels cake.]]>
</summary>
</item>
by adding the <br />
inside the the <![CDATA]]>
this allows the line to break, thus creating a new line!
Related Topics
Alter $Path in Vim/Macvim So as to Find the Right Ruby Binary
Better Way to Write "Matching Balanced Parenthesis" Program in Ruby
Bitwise Operations on Strings with Ruby
How to Modify a Text File in Ruby
Is There a Pry Debug Setup That Works with Ruby 2.0
How to Express 'Infinite Time'
No Implicit Conversion from Nil to Integer - When Trying to Add Anything to Array
Activeadmin Custom Views Which Retain the Activeadmin Layout
How to Update to Ruby 2.1.2 Using Rails 3.2.3
Ruby on Rails Add a Column After a Specific Column Name
Rails: How to Print a Decimal as a Percent
Fastercsv Error with Ruby 1.9.2
How to Know the Current Rake Task
Ruby Can Not Access Variable Outside the Method