This question already has answers here:
Preserve newline in text area with Ruby on Rails
(3 answers)
Closed 8 years ago.
It seems like rails treat every post with line break by change the line break into a space.
for example:
when I type
hi
hi
hi
in my form, what I get is just a simple "hi hi hi". How to change that?
I assume that in post form for description field you may have taken textarea
as Rustam commented you can use simple_format helper method: Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags.
You can use like
<%= simple_format(#post.description) %>
I hope this work out for you..
Related
This question already has answers here:
Accessing a Session object from Razor _Layout.cshml
(2 answers)
Closed 7 years ago.
I am trying to add a session variable to html markup using MVC Razor, I tried this:
<div class="panel-body">
<p>It might suprise you to know that you can upgrade from #Session("CurrentProvider") to blah blah....</p>
</div>
I tried wrapping it in code tags and all sorts. How can I fix this?
Seems the answer is to append with ToString
#Session("CurrentProvider").ToString
This question already has answers here:
Rendering a Rails view to string for email
(4 answers)
Closed 8 years ago.
I need to get the HTML code of the current page in Rails.
In the controller's action I need a string with that HTML code, for example:
<html><head></head><body><p>Hello</p></body></html>
The problem looks easy, but I can't find the solution.
If you need to render the output of an action as a string:
html = render_to_string(...)
It generally takes the same options as render but gives you the result you can work with instead of sending it to the client.
This question already has answers here:
What does <<DESC mean in ruby?
(3 answers)
Closed 8 years ago.
I saw the following code in deviser_helper.rb. What does <<-HTML ... HTML mean here:
html = <<-HTML
<div id="error_explanation" class="alert">
<h2>#{sentence}</h2>
<ul>#{messages}</ul>
</div>
HTML
Surely a great way to write embedded HTML code in ruby. But why this works and where it comes from?
This is just a multiline string in Ruby. Usually it's called a heredoc or here document. From the linked documentation:
Following a << you can specify a string or an identifier to terminate the string literal, and all lines following the current line up to the terminator are the value of the string.
The - after << means that you can indent the terminator, so that the HTML at the end can have spaces or tabs before it.
This question already has answers here:
Embedding Ruby code in HTML?
(3 answers)
Closed 9 years ago.
Is it possible to put Ruby code in the HTML page which has a extension ".rhtml"?
For example, I want to use a HTML form and then some Ruby code. Does it have to be encapsulated in the <% tags?
The file extension should be ".html.erb" for Rails 3 or above.
The Ruby code should be between <% %> for logic statements, or between <%= %> if you want to display the results of the code.
".erb" is what you are looking for. Rails uses ".erb" files and it is possible to embed Ruby code inside them.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between -%> and %> in rails
I need to know what is the difference between <%= expression %> and <%= expression -%> on rails, please help me to make good foundation on Ruby On Rails
The '-%>' means, that no linebreak will be appended to the output of the expression. It's usefull if you want to control the amount of whitespace you have in the generated HTML but do not want to put all the code in a single line.
As mentioned before, the -%> is used to control output whitespace. I you're at all concerned with how your HTML looks, use HAML. HAML is way more clear and readable when coding and it generates clear, valid formatted HTML. No more forgotten close tags!
I say don't bother with '-%>' If you are using layouts and partials with your views it is difficult control the output anyway, things like indentation will likely be messed up. Just focus making your ERb look good and don't worry too much what the generated output looks like.