Render HTML into Rails flash message - html

How to render html into Rails flash message/notice ?
I have this destroy action from my UsersController :
def destroy
User.find(params[:id]).destroy
flash[:success] = "<i class=\"fa fa-check\"></i> Utilisateur supprimé.".html_safe
redirect_to users_url
end
When I call this destroy action, the flash success message print the html tag instead of render it.
I have try with this answer but I don't how to use it since I am in the controller and not in the view so I am not using <%= %> to embed the code.

If someone encounter the same problem, here is an answer working for me :
1st : the .html_safe method has to be applied to the message of the flash as Justin Licata said.
2nd : if you want to use icons (Font awesome in my case) into your message, putting it into your message string will not work, you have to put it into your .html file which print your flash message. (Inspired from this)
Here is my rendering flash message code, into my layout application.html.erb :
<% flash.each do |message_type, message| %>
<div class="row" id="msgContainer">
<div class="callout callout-<%= message_type %>" id="flash_message">
<% if flash[:notice] %>
<i class="fa fa-info"></i>
<% end %>
<% if flash[:danger] %>
<i class="fa fa-exclamation-triangle"></i>
<% end %>
<% if flash[:success] %>
<i class="fa fa-check"></i>
<% end %>
<%= message.html_safe %>
</div>
</div>
<% end %>
This work for me, hope this can help.

Related

Some HTML for values of Hash keys

I am building a panel with a partial, but I'd like to customise the inside of the panel with some html. Is there any way to write some HTML for a hash value?
I have a custom partial _panel_builder.html.erb that I takes as argument pclass, heading, body, etc., that I would like to use like this :
(The below syntax is bad, but I don't really understand how I could do something nice..)
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
# For the body param, I'd like to be able to use some HTML with occasional <%=...%> for variables, like :
body: (%>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<%).html_safe,
collapsable: true %>
<% end %>
EDIT : An idea of what my _panel_builder partial looks like :
<%
collapsable ||= false
pclass ||= "default"
footer ||= false
%>
<div class="panel panel-<%= pclass %> <%= if collapsable then "panel-collapsable " end %>">
<div class="panel-heading">
<%= heading %>
<% if collapsable %>
<span class="pull-right"><i class="glyphicon glyphicon-chevron-down"></i></span>
<% end %>
</div>
<div class="panel-body <%= if collapsable then "collapse" end %>">
<%= body %>
</div>
<% if footer %>
<div class="panel-footer <%= if collapsable then "collapse" end %>">
<%= footer %>
</div>
<% end %>
</div>
Okay so I was actually looking for the capture helper :
<% #etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
body: capture do %>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<% end %>,
collapsible: true %>
<% end %>
Note : in some cases, I also want to pass a complex HTML block as the header of footer, so I can't just have a helper that takes a block.
Note2 : My panel is a HTML template made for generic data. I cannot pass it a Ruby object

How to link_to an html anchor tag

I have been trying to make a number of lists where after clicking each list its content gets edited. I'am using twitter bootstrap, embedded HTML in this Ruby on Rails app.
<div class="list-group">
<% #statuses.each do |status| %>
<%= status.content %>
<% end %>
</div>
Here i did not get how to get these <%= link_to to get connected with each <a href="" URL's of the status.
<%= link_to 'Edit', edit_status_path(status) %>
Please help i m totally confused.
Thanks in advance.
If you want the entire status to actually be a link, like you did with a manual anchor tag in your example, then try:
<div class="list-group">
<% #statuses.each do |status| %>
<%= link_to status.content, edit_status_path(status), class: "list-group-item" %>
<% end %>
</div>
Also see http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to.

inserting ruby code breaks html

I have this code with a check method that returns true or false
<%= link_to path, format: 'js' do %>
<i class="<%= check ? "close\"></i>Unfollow member" : "open\"></i>Follow member></i>" %>
<% end %>
</div>
<div class="list-group list-normal m-b-none">
but this outputs
<i class="close"></i>Unfollow member
</a> </div>
<div class=" list-group="" list-normal="" m-b-none"="">
</i>
How can it be ? (I don't want to repeat the check statement inside and outside the i tag)
I think following will be more readable:
<% if check %>
<i class="close">Unfollow member</i>
<% else %>
<i class="open">Follow member></i>
<% end %>
Update:
To make it one liner:
<%= check ? "<i class='close'>Unfollow member</i>" : "<i class='open'>Follow member></i>" %>
<%= link_to path, format: 'js' do %>
<i class="<%= check ? "'close'></i>Unfollow member" : "'open'></i>Follow member>" %></i>
<% end %>
I did not try. But if may works.

Haml to HTML/erb...can anyone see the mistake I'm making

I forked a Rails project that uses Haml, which I haven't really learned. There's an online converter that helped me with the HTML, but it ignores the erb, and when I tested my own conversion, the flash messages didn't have the classes associated with them, so I know I did it wrong, but I can't see why.
Note (don't worry about spacing in the haml. in the code i have
Original haml
- if flash[:notice]
.alert-message.warning
%p
= flash[:notice]
- if flash[:error]
.alert-message.error
%p
= flash[:error]
faulty Html translation
<% if flash[:notice] %>
<div class="alert-message.warning">
<p>
<%= flash[:notice] %>
</p>
</div>
<% end %>
<% if flash[:error] %>
<div class="alert-message.error">
<p>
<%= flash[:error] %>
</p>
</div>
<% end %>
You have a huge indentation after first line. It should be 2 spaces.
edited: In case you need to convert small part of haml you can learn how to convert it manually. There is the little haml tutorial where you can learn some basics.
That part of haml can be turned into following erb:
<% if flash[:notice] %>
<div class="alert-message warning">
<p><%= flash[:notice] %></p>
</div>
<% end %>
<% if flash[:error] %>
<div class="alert-message error">
<p><%= flash[:error] %></p>
</div>
<% end %>
where:
- same as <%
= same as <%=
%p (%tag_name) -> <p>
.class -> %div.class -> <div class="class"
edited#2:
judging by generated html <div class="alert-message.warning"> should be <div class="alert-message warning"> (dot replaced by space between classes)

html inside rails' if statement

In my example I want RoR to display an image when I'm listening to Pearl Jam.
Winamp writes 'currently playing' info to np.txt.
<%= data = File.read("np.txt")
if data.include? "Pearl Jam"
<img src="space.jpg" alt="sagan"/>
end
%>
However I'm not sure how to get HTML tags to work inside RoR code.
I think what you want is:
<% data = File.read "np.txt" %>
<% if data.include? "Pearl Jam" %>
<img src="space.jpg" alt="sagan"/>
<% end %>
In ERB anything rendered outside of the <% %> tags is HTML.
ian.
You should use an image tag.
<%- data = File.read "np.txt" -%>
<%= image_tag("space.jpg", :alt => "sagan") if data.include? "Pearl Jam" %>