inserting ruby code breaks html - 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.

Related

Render HTML into Rails flash message

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.

IF/ELSE Conditions on Ruby hash

I want to set up a Ruby partial that takes a 'type' then, depending on that type, spits out a <li> with defined elements (such as an icon, a particular label, etc)
Here's my working .erb chunk
<ul>
<% if locals.has_key? :sermon_links %>
<% sermon_links.each do |link| %>
<% if locals.has_key? :type == "download" %>
<li>
<i class="material-icons">music_note</i>
Download this sermon (~5mb)
</li>
<% else %>
<% if locals.has_key? :type == "passage" %>
<li>
<i class="material-icons">link</i>
<%= sermon_passage %> on Bible Gateway
</li>
<% end %>
<% end %>
<% end %>
</ul>
And then I'd call it in the HTML file like this:
:sermon_links => [
{ :type => "download", :hyperlink => "https://www.biblegateway.com/resources/matthew-henry/John" }
]
I'm 99% sure the problem is how I'm setting the 'IF type equals THIS' (<% if locals.has_key? :type == "download" %>) I'm a newbie to Ruby on Rails so any help in this area would be really appreciated :) thanks!
I think you can rewrite as follows:
<ul>
<% if locals.has_key? :sermon_links %>
<% sermon_links.each do |link| %>
<% if link[:type].present? && link[:type] == "download" %>
<li>
<i class="material-icons">music_note</i>
Download this sermon (~5mb)
</li>
<% else %>
<% if link[:type].present? && link[:type] == "passage" %>
<li>
<i class="material-icons">link</i>
<%= sermon_passage %> on Bible Gateway
</li>
<% end %>
<% end %>
<% end %>
<% end %>
</ul>

Embedding html tags inside rails tag

I am trying to embed html tag inside rails tag but they are not working .I am even using it with html_safe still they are not working.Here is my code
<li class="blue">
<span>
<%= link_to "#{if user.status == "Active"
then "Status Change"
'<i class="fa fa-eye"></i>'
else "Status Change"
'<i class="fa fa-eye"></i>'
end}","/users/change_status?id="+user.id.to_s %>
</span>
</li>
I am not getting the desired output .It is printing
<`i class="fa fa-eye">
this on the screen
Try:
<li class="blue">
<span>
<%= link_to "/users/change_status?id=#{user.id}" do %>
<% if user.status == "Active" %>
Status Change <i class="fa fa-eye"></i>
<% else %>
Status Change <i class="fa fa-eye"></i>
<% end %>
<% end %>
</span>
</li>
and, for
<%= link_to "/users/change_status?id=#{user.id}" do %>
<% end %>
part, you can use something like this:
<%= link_to change_status_users_path(id: user.id) do %>
<% end %>
Try it with the block notation
<%= link_to 'Status Change' do %>
<i class="fa fa-eye"></i>
<% end %>
I removed the conditionals to make my point clearer.
Try this
'Status Change <i class="fa fa-eye"></i>'.html_safe

How to rewrite HTML for Rails

I'm trying to re-write the code below so that it will use an image_tag. I know how to apply a class attribute, but how do I apply a custom attribute like "data-dark-logo"?
I'm learning how to develop and am building an application that has the following HTML:
<div id="logo">
<a href="index.html" class="standard-logo" data-dark-logo="external_assets/images/logo-dark.png">
<img src="external_assets/images/logo.png" alt="Canvas Logo">
</a>
<a href="index.html" class="retina-logo" data-dark-logo="external_assets/images/logo-dark#2x.png">
<img src="external_assets/images/logo#2x.png" alt="Canvas Logo">
</a>
</div>
I'm trying to rewrite this so it will function in my Rails app.
Just pass the data attributes as a hash.
<%= link_to '/link', class: 'standard-logo', data: {'dark-logo' => 'external_assets/....' do %>
<%= image_tag ('external_assets/...') %>
<% end %>
Like this?
<div id="logo">
<%= link_to("index.html", class: "standard-logo", data-dark-logo: "external_assets/images/logo-dark.png") do %>
<%= image_tag("external_assets/images/logo.png", alt: "Canvas Logo") %>
<% end %>
<%= link_to("index.html", class: "retina-logo", data-dark-logo: "external_assets/images/logo-dark#2x.png") do %>
<%= image_tag("external_assets/images/logo#2x.png", alt: "Canvas Logo") %>
<% end %>
</div>
Edit:
To make the data-dark-logo pull your asset from the asset pipeline, try using the asset_path helper:
<%= link_to("index.html", class: "standard-logo", data-dark-logo: asset_path("external_assets/images/logo-dark.png")) do %>
The solution I came up with looks like this:
<div id="logo">
<%= link_to welcome_index_path, class: 'standard-logo', data: {'dark-logo' => asset_path("front/logo-dark.png") } do %>
<%= image_tag("front/logo.png") %>
<% end %>
<%= link_to welcome_index_path, class: 'retina-logo', data: {'dark-logo' => asset_path("front/logo-dark#2x.png") } do %>
<%= image_tag("front/logo#2x.png") %>
<% end %>
</div>
But I'm not sure if this complies with Rails best practices

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