How to use <br /> in a placeholder on Rails 3? - ruby-on-rails-3

I have a textarea which is has Markdown support and I would like to show a placeholder to show what sort of formatting Markdown likes but putting <br /> in the placeholder shows <br /> in the text. I would prefer it to make a new line if this is possible.
I am using Rails 3, below is the code I am using:
<%= f.text_area :info, :placeholder => "if you want you can <br /> add a link by doing this: [text](http://link.com) it's pretty neat aye? or you could use bold by doing this: **bold text is cool**" %>

According to the specification, the placeholder attribute can't contain any line breaks or carriage returns.

As Logan says, though you can find some hacks in the similar question here:
Can you have multiline HTML5 placeholder text in a <textarea>?

Here are some more related hacks to multiline placeholders, I followed the example in the answer below to do my workaround:
Insert line break inside placeholder attribute of a textarea?
For rails, I thought this was useful if you want a gem to deal with placeholders:
jquery-placeholder-rails.
However, you'll need the forked version of jQuery-placeholder that supports newline: jQuery-Placeholder-Newlines

Related

How to use br tag in haml?

I have been trying <br> tag in haml, very unfortunately none of my code are working. How should we use nest for <br> in haml ?
%h1 Helo mate
%br/
whrere are you ?
Your example (%br/) already seems to be correct.
Whether you get a selfclosing tag (<br />) or a standalone tag (<br>) depends on whether your code is interpreted as html or as xhtml, so check which format you need. Xhtml has problems with non-closing tags.
Look here for more info.
Edit: Adding the info from matt's comment. The problem is not the br tag, but the content of the h1 tag being on the same line as the tag as well as on the next line, while the whole content should be nested when the content is more than one line:
%h1
Hello mate
%br/
where are you?
%h1
Hello mate
%br where are you

Using Polymer conditional attributes with HAML

According to the documentation for Polymer expressions, you can bind data to conditionally assign an attribute using the conditional attribute syntax:
Conditional attributes
For boolean attributes, you can control whether or not the attribute
appears using the special conditional attribute syntax:
attribute?={{boolean-expression}}
That's great, but I'm using HAML where attributes are assigned to elements like this:
%element{attribute: "value"}
I can't add a question mark before that colon without HAML giving me a syntax error.
So how can I use Polymer's conditional attributes (or a functional equivalent) when I'm using HAML to generate my HTML?
One potential solution is to use the :plain filter to insert raw HTML into your HAML file:
:plain
<element attribute?={{boolean-expression}}></element>
A bit ugly, but it seems to work.
If you need to enclose some HAML-generated tags in one of these plain HTML tags, you'll need to use the :plain filter twice; once for the opening tag, and once for the closing tag.
:plain
<element attribute?={{boolean-expression}}>
-# HAML Content Here
:plain
</element>
Be sure not to indent your HAML code after the opening tag, otherwise it will become part of the "raw HTML" output and get sent as plain text to the browser instead of being processed as HAML.
The current version of HAML (4.0.6) supports conditional attributes:
%core-menu{hidden?: '{{!globals.current_series_detail}}'}
Make sure you're not putting a space before the question mark.

Bootstrap typeahead avoid other value

i still try in rails 3.2 to use bootstrap typeahead, it works fine, but how can i avoid users to type anything in text field.
I code looks like this
<%= text_field_tag(:text, "", :data => {:provide => "typeahead", :items=>"4", :source=>'["Swiss","German","English"]'}) %>
users may only type Swiss, German and English in the text field, but not for example Chinese.
How can i validate the value in text field?
Set tag attribute disabled="disabled"

How to make html code in erb tag not escaped

I have some simple erb code in one of my views in a rails project.
<%= comment.body %>
I'd like the html tags in the comment.body to be preserved as they have formatting information. I've verified that the text is saved in the database properly like
<b>hello</b>
However it turns out on the page to be <b>hello</b> not hello as I expect.
How could this be? I'm not using <%= h to escape the html code.
How do I make it not escaping? I'm using rails 3. Does this matter?
You can also use sanitize.
<%= sanitize(comment.body) %>
sanitize will leave html code but escape javascript.
Rails 3 now automatically escapes your output.
To unescape the text and use the actual tags, use raw(...):
<%= raw(comment.body) %>
However, be careful with this, as it will allow any tags, including scripts (potentially malicious). A safer option might be to have users use markdown-formatted text or something similar, rather than allowing raw HTML tags.

Disable HTML escaping in erb templates

In a Rails 3 application I have a domain class where one attribute stores pure HTML content (it's a blog app, the domain class is Post).
In the ERB templates, I need to display the content of the attribute as it was formmated, with the HTML tags in place. But, Rails is escaping all HTML tags! How can I disable this behaviour for this class attribute?
Example:
somePost = Post.new
somePost.content = "<strong> Hi, i'm here! </strong>"
In the erb template:
<%= somePost.content %>
The HTML generated is escaped:
<strong> Hi, i'm here! </strong>
Try using raw(somePost.content). Alternatively, somePost.content.html_safe.
Use raw(string), as described in the release notes.
7.4.3 Other Changes
You no longer need to call h(string) to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call raw(string).
Basically, where you did
<%=h #model.attr %>
before you can now use
<%= #model.attr %>
and where you did that before you can now use
<%=raw #model.attr %>
Using a double equals means the result is not escaped...
<%== somePost.content %>
See this SO question about it - What does <%== %> do in rails erb?