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.
Related
This question already has answers here:
How do I add HTML code to JSF FacesMessage
(4 answers)
Closed 7 years ago.
I need to add a Html content when some method returns the wanted result (any method, any result) to the JSF page exactly like primefaces do with .
Html to add is simple :
<div class="alert alert-success" role="alert">
<strong>Well done!</strong> You successfully read this important alert message.
</div>
But can't find a way to do this cleanly, I've read many posts like How do I return HTML from managed bean in JSF? but my bean need to insert that html when user is doing something e.g. submitting a form result.
Also : is there a way to save it and show it in case of a redirection like it is with primefaces context.getExternalContext().getFlash().setKeepMessages(true);
Why not simply using h:messages tag ? It is fully customizable in terms of design and you can simply affect your styles and class without all the turn-around
Source : http://www.jsftoolbox.com/documentation/help/12-TagReference/html/h_messages.html
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:
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..
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I barely use them in my projects. Is this normal? If I need to return just one line of html, say an image tag for a gravatar, then a helper is great:
def gravatar_for(user, height=90, width=90, alt=user.name + "'s gravtatar")
gravatar_address = 'http://1.gravatar.com/avatar/'
clean_email = user.email.strip.downcase
hash = Digest::MD5.hexdigest(clean_email)
image_tag gravatar_address + hash, height: height, width: width, alt: alt
end
However, if I want to loop through some errors, and return html structure rather than a single line, it quickly gets difficult.
Is a good rule of thumb if you want to return one line of html then use a helper, else use a partial to return more complex html?
First of all, the question is opinion based. So, most likely it will be closed.
Here are several questions on similar subject:
DRYing rails view: partial vs helper
Why shouldn't Helpers have html in them?
Generally speaking, helper is just a way to
Have DRY views
Make clean views and controllers
Generally speaking you can DRY views by moving out some duplicated functionality to controllers. However, as result controllers got cluttered. So, the better way to keep view related helpers in helpers (vs controllers), which you already figure out.
Some Helpers don't touch HTML at all (as example, they may just provide some formatting of numbers etc) or they may generate just one tag (as in your example) or generate piece of HTML code. However, as soon as you are talking about some piece of HTML code, you rather use partials vs helper.