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.
Related
My .erb file does not execute the code inside <%= %> as ruby code. For example::
<%= puts "almost"%>, when rendered is just <%= puts "almost"%>. The erb engine does not even process anything. This means that NOTHING inside <%= %> is being processed .Any help will be appreciated.
You are confusing erb expressions with Ruby code. Use <% %> to have arbitrary Ruby code executed.
<% puts "almost" %>
Anything inside <% %> will be evaluated as ruby code, and anything inside <%= %> will be outputed as html. So when you do <%= puts "almost" %> it is outputded directly as HTML. You can solve this using
<% puts "almost" %>
or
<%= "almost" %>.
I wanted to use two form_tag in rails like this,
<%= form_tag %>
<%= form_tag %>
................
<%= submit_tag %>
<% submit_tag %>
But the action for inside form_tag taking first form_tag's action...!!!
For both form_tag assign the different action?
HTML does not allow nested forms, thus it's reasonable that the helpers are not designed to support nesting.
what you need is called *Nested Forms * take a look at the rails cast #196 Nested Model Form Part 1
Here is a link to a gem that you can use to help you out https://github.com/ryanb/nested_form
i have the t function witch return some text.
so on my erb file i have something like that:
<%= t 'content1' %>
<%= t 'content2' %>
and the html output is something like that:
"text of the content 1"
"text of the content 2"
I would like to output something like that:
"text of the content 1" "text of the content 2"
thanx
ERB has an option in the closing tag whether to have a newline after it. Just add a dash:
<%= t 'content1' -%>
<%= t 'content2' %>
Erb is a little overrated compared to other template languages out there. But, the good thing is it allows you to control the output. As #DMG pointed out add a '-' to your tags but there is also another point I wanted to show.
<%- method(...) -%>
Will not show any lines on output. While this will create a blank line:
<% method(...) -%>
Just something to keep in mind if your writing templates for scripts, unit tests, rake task, or anything else outside of Rails/ActionView
<%= "#{t('content1')} #{t('content2')}" %>
just do:
<%= "#{t('content1')}#{t('content2')}" %>
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.
I am working on a code base which as VBScript code embedded in HTML. I've noticed the following two different tags around said lines of code
<%= MyFunc(val1) %>
and
<% MyFunc(val1) %>
What is the difference in using the "=" character at the beginning of these sections?
<% evaluates an expression in server code but doesn't emit output.
<%= also evaluates the expression but wraps the result in Response.Write, so it produces output.
When you see:
<%= MyFunc() %>
it really means:
<%
Response.Write( MyFunc() )
%>
Its short hand for writting output to the response.
<%
MyFunc()
%>
The above will just run the code but won't write it to the response unless it has some Response.Write's inside the Function/Sub itself.