Using Ruby and HTML together [duplicate] - ruby-on-rails

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.

Related

How do I add session variable to view markup? [duplicate]

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

How to summit a post with line break in rails? [duplicate]

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..

Get raw HTML on current page [duplicate]

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.

How to add <%= %> & variants in vim insert mode while using vim-rails and vim-surround? [duplicate]

This question already has answers here:
VIM: insert empty ERB tags
(2 answers)
Closed 8 years ago.
Is there any vim mapping/plugin that produces the following behaviour in an .erb file while having vim-rails and vim-surround installed?
mapping/key-press [normal mode]
<%= *cursor position* %> [insert mode]
Same with <% %> and <%# %>.
Note: I know I can just surround it with surround.vim after writing it. I want to know the reverse way.
That way I get syntax highlighting while writing the inner ruby code.
You are looking for RagTag or Rails plugins (in addition to surround) which provides the surroundings you need. You would use Rails.vim if you are using rails and RagTag otherwise. You can have both installed if needed.
Once you have surround and RagTag/Rails installed you can just do the following:
<c-s>= insert mode for <%= %>
<c-s># insert mode for <%# %>
Visually select code and do S= to surround with <%= and %>
Note: You may have to use <c-g>s for insert mode surrounding if you are using a the terminal. You could also disable your terminal flow control by running stty -ixon which would allow you to use the <c-s>/<c-q> keys (I personally disable flow control in my ~/.bashrc).
If you want to add "surroundings" to other filetypes please read :h surround-customizing.
Please read both the surround documentation and whichever plugin you decide to install for more details.

Difference between <%= expression %> and <%= expression -%> on Ruby On Rails [duplicate]

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.

Resources