I have a Rails app (blog) that I am creating. Very basic stuff. In my content area I have a text area for the content of the post. I am needing to include some html in the text area (links, formating, etc).
<%= f.text_area :content %>
Is there another tag that I can use instead of text_area, that will allow me to do this?
Have you tried this in the views?
<%= content.html_safe %>
This is an old question but I guess there's a pretty straight forward view helper for this: sanitize. I presume you'd want to render the HTML tags entered by the user. For that purpose, save the content as a string and render as HTML using sanitize.
Example usage:
sanitize #post, tags: %w(strong em a code pre h2 h3 p blockquote ul ol li br),
attributes: %w(href class)
The tags option allows you to specify which tags to use and same with the html attributes.
The HTML safe method is actually .html_safe. Just tested on a text field.
For example:
<%= #item.description.html_safe %>
Are you looking for something similar to the bold, italic, ... options you get when posting in stackoverflow? If so, I would suggest Markitup, a text-editor plugin for jQuery. Once you get this set-up, you'll be able to enter mark up in your text area (e.g. Markdown, bbcode, ...). When you actually display the result on the page, you simply need to have Ruby parse the mark up language you chose.
E.g.
<%= #user.bio.bb_code %>
Using this method, you allow your users enter styled text in a safe fashion.
With the forthcoming Rails 6, you will be able to use a new rich_text_area tag to create a rich text editor in your forms like this:
<%= form_with(model: article) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.rich_text_area :content %>
</div>
<% end %>
See Action Text Rails Guide
text_area_tag is probably what you want.
Related
Note: I am a Ruby on Rails newbie so excuse this question if it seems too obvious. But for the life of me, I can't figure out where to add an HTML break tag to this Ruby code to make the elements stack on top of each other, not beside each other.
<p>
<strong>Genres:</strong>
<%= #book.authors.map {|a| a.name}.join(', ') %>
</p>
I have added the break tag after the %> and before it, inside the join method area with the comma, and everywhere else you can imagine and nothing works, it just places the data results beside each other sequentially. I am sure it's something simply but I can't figure it out.
Any help on this would be appreciated.
The typical Rails way to do this is with a for or each loop:
<p>
<strong>Authors:</strong>
<% for author in #book.authors %>
<%= author.name %><br/>
<% end %>
</p>
The for statement could also be #book.authors.each do |author|. From an HTML and CSS perspective, I would argue that it is much better to put each author in their own container element such as <li>, which can be styled to look the way you want.
I try to use scaffolding in rails to build a blog. I save my blog's content as :text.
However, when I see the view. It can only show in one line. It looks not good.
How can I solve this problem?
Your database stores text, not HTML. In HTML line breaks are ignored unless they are marked up with relevant HTML tags. For example, a break between two paragraphs should be marked by the end of one <p> element and the beginning of another, and a single line break should be represented by a <br> tag.
Rails' simple_format helper converts text to HTML by replacing line breaks with HTML tags.
So instead of something like this:
<p><%= #post.content %></p>
You would do this:
<%= simple_format #post.content %>
I'm doing a sort of blog in Rails, and when I put something like
Hello, <b> how are you? </b>
inside the text_area and I send it, I see that it was saved as a plain text, and the HTML inside it is not recognized.
The only thing I want is that every single HTML tag that I put in the text_area be interpreted as HTML and not as a plain text.
For example, the first code should be shown in my page like this:
Hello, how are you?
This is the code of the post's view new (in HAML):
= form_for #post do |f|
.form
.flabel= f.label :title
= f.text_field :title
.flabel= f.label :content
= f.text_area :content
.submit= f.submit "Create"
How could I do this without HTMLArea or things like that?
Thank you!!
What about raw helper? Try the folliwing <%= raw #post.content %> in your view.
I think <%= #post.content.html_safe %> would also work.
I have a blog that saves input from html input tags, saves them as XML into a sqlite3 database, and then finally renders the content inside of this tag.
<p>
<b>Content:</b>
<%= #post.content %>
</p>
I need this .content to be able to render HTML. Any ideas?
For these kind of situations, you have to use either:
Markup language like RedCloth,
Or an editor like CKEditor.
Using CKEditor is a breeze with the CKEditor gem
And please note if you are using Rails 3, you need to explicitly say you want raw HTML, like this:
<p>
<b>Content:</b>
<%= raw #post.content %>
</p>
Should I be using the content_tag helper for all html tags when working with Rails?
Is it The Rails Way to use content_tag for even simple things like Header tags?
<%= content_tag :h2, :class => "bla bla" do %>
Header
<% end %>
vs.
<h2>Header</h2>
Clearly just using straight html is much 'simpler' and 'shorter', but what is the correct Rails Way of doing things?
Using content_tag when you don't have to is a waste. There's no need to use ERBisms to generate static HTML so don't do it. If some other piece of code determines what tag to use, then you'd use content_tag to construct that tag.
If you are asking the rails way of doing this, then its defiantly using 'content_tag', but using tag_helpers has its own advantages and disadvantages
Personally for me I can see these things, (Using rails helpers instead of pure HTML)
Advantages
1 - Your code will be cleaner. (with less lines)
2 - You will have more control other the elements.
Ex: You can have your own helper tags like 'big_text_box' which will return a text box more than the normal with and you can use it across all the site
3 - You will be able to add attributes like class, id dynamically in the runtime
Disadvantages
1 - If you have a separate designer (I mean UI engineer) he/she will get confuse of by the code you have use. As its not pure html
2 - Its slow than the pure html (But this will not even noticeable unless otherwise your app is a damn major one...)
So its up to you to decide what to use, personally I prefer using rails helper tags as it makes me more comfortable
HTH
cheers
sameera
One useful method is the "div_for", which is somewhat similar to the content_tag. If you find yourself marking up HTML elements with data you can reference later, "div_for" makes your life much easier.
Let's say you have a bunch of people being shown on a page and you need to wrap each with a div that has a unique ID so you can modify these elements with JS. By hand and straight HTML you would have to do:
<% #people.each do |p| %>
<div id="person_<%= p.id %>"><%= p.name %></div>
<% end %>
That would get bothersome if you were doing LOTS of this with multiple attributes (I commonly use custom IDs, classes, and some data attributes). But using div_for you could write the above as:
<% #people.each do |p| %>
<%= div_for(p) do %><%= #person.name %><% end %>
<% end %>
Makes the HTML a little easier to read when things get long and complex. I found it is much cleaner when working with javascript a lot.
http://apidock.com/rails/ActionView/Helpers/RecordTagHelper/div_for