How do I use HTML Data- Attributes in Rails? - ruby-on-rails

I have seen this question asked in Stack Overflow but I have been unable to understand the answers given. Could someone please rewrite what I have the proper way so that I can just look at it and understand what you've done.
Currently I have this HTML link that opens a lightbox and displays an image.
<li><i class="fa fa-eye"></i></li>
However, since moving it into rails it doesn't work and I am wondering how I would rewrite this so that it does work.

<%= content_tag :li do %>
<%= link_to fa_icon("eye"), "images/product/image1.png", data: { lightbox: "example-set" } %>
<% end %>
This is assuming you're using the font-awesome-rails gem, which comes with the fa_icon helper.

Related

Kaminari pagination in Rails 4 with glyphicon for links

I'm using the Kaminari gem for pagination in Rails 4. I want to use the < and > glyphicons (chevron-right and chevron-left) on the side of the pages to go to previous and next page, and can't figure out how to do that. Here's what I have so far:
To make a glyphicon into a link, I can use this:
<%= link_to "some_link" do %>
<i class="glyphicon glyphicon-chevron-right" title="Next Page"></i>
<% end %>
Kaminari also comes with <%= link_to_next_page #pages, 'Next Page' %> but that displays text on the page, not an image
Is there a good way to implement this?
Customize the views/kaminari/_next_page.html.erb in Kaminari
Remove this part of the link_to
raw(t 'views.pagination.next')
and replace with
'<i class="glyphicon glyphicon-chevron-right"></i>'.html_safe
Update
Make sure you have run this so you see the views to modify.
rails generate kaminari:views
For Bootstrap...
rails generate kaminari:views bootstrap
The solution is simple; Using the logic in the explanation Beengie gave, I just needed to include some raw HTML in the link using 'MY_HTML'.html_safe
<%= link_to_next_page #records, '<i class="glyphicon glyphicon-chevron-right"></i>'.html_safe %>
<%= link_to_previous_page #records, '<i class="glyphicon glyphicon-chevron-left"></i>'.html_safe %>

Rails - Use a Helper or a Partial [duplicate]

This question already has answers here:
When to use Helpers instead of Partials
(6 answers)
Closed 7 years ago.
In my Article model, I have 4 different columns: short_effects, long_effects, benefits, and alternatives. They are all text columns.
In my view, I am calling each one in the same basic format:
<% unless #article.short_effects.blank? %>
<hr id="effects">
<h2><span class="glyphicon glyphicon-hourglass"></span>Short-term side effects</h2>
<ul>
<% #article.short_effects.split(';').each do |effect| %>
<li><%= effect.downcase %></li>
<% end %>
</ul>
<% end %>
<% unless #article.long_effects.blank? %>
<hr id="effects2">
<h2><span class="glyphicon glyphicon-time"></span>Long-term side effects</h2>
<ul>
<% #article.long_effects.split(';').each do |effect| %>
<li><%= effect.downcase %></li>
<% end %>
</ul>
<% end %>
.
.
.
to eliminate repetition, I would like to create a helper or a partial... however, I do not know which is better for this situation. I know some people frown upon HTML inside of helpers, but since helpers are used to reduce code from views, I feel that may be the best way. Any advice is greatly appreciated.
There are some existing discussions about this one here. In particular, this question has a variety of (mostly short) answers with some good rules-of-thumb you might consider. I would personally be of the "minimize HTML in a helper" school of thought, however I also like the description of helpers being for specific elements while using partials for larger chunks of code.

Is It Possible To Use Variables To Build a link_to Command? (Rails 3.2.11)

I am creating a section of my Rails application for the visually impaired. This requires me to create it using only text and links in order to make it easier for people using speech readers to navigate through. I want to use fields from an existing model to dynamically build a link_to command. I would like to be able to build a variable using several fields on the model that contains the text that a user clicks on and another field from the model which contains the link.
Here is the code in my controller:
MediaLibrary.find(:all, conditions: ["media_type_id < ?", 3], limit: 5).each do |media_item|
#audio_links["link_text"] = "Audio of #{MediaCreator.find(media_item.media_creator_id).name} #{media_item.media_created.to_time.strftime('%A, %B %d, %Y')} at #{media_item.meeting_time}
#{media_item.am_pm} - #{media_item.name}"
#audio_links["link"] = media_item.link
end
Here is the code in my view:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to audio_link["link_text"], '#{audio_link["link"]}' %>
</li>
<% end %>
I have also tried the following:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to 'audio_link["link_text"]', '#{audio_link["link"]}' %>
</li>
<% end %>
And this:
<% #audio_links.each do |audio_link| %>
<li>
<%= link_to '#{audio_link["link_text"]}', '#{audio_link["link"]}' %>
</li>
<% end %>
I have tried a few more variations but I either get the can't convert String into Integer error on the link_to command when I attempt to display the screen or the links display with the text being displayed as the following. When this happens I get other errors when I click the link.
#{audio_link["link_text"]}
I have done a lot of searches on Stack Overflow and throughout the web. I have not found a single example of this being done anywhere. I have seen in older posts where there was a set_path command (2010) but nothing for recent posts. I have used html_safe! before and will add that to my code. I do not know if there is a problem with my code or if I am attempting something that is not possible. I sincerely hope this is possible because it will make it easier for people with speech readers to know what they are clicking on.
Any help with this would be appreciated.
You can't do string interpolation in single quotes. Replace the single quotes with double quotes and your variables will be expanded properly.
For example:
<%= link_to audio_link["link_text"], "#{audio_link['link']}" %>

Link_to a page - Ruby on Rails

I have the following code:
<% slika = Refinery::Page.find('sladoledi') %>
<%= link_to (image_tag slika.key_image.url, slika) %>
The problem is that it's not linking to slika. Any suggestions?
Try this format
<%= link_to(slika) do %>
<%= image_tag(slika.key_image.url)%>
<% end %>
also have a look at documentation there are nice examples how to use link_to() helper
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
First start nesting code you write here. People have problem reading code like this :(
If you want add code click code button on editor.
I suppose the problem is you don't end image_tag.
Correct form is:
link_to(image_tag(slika.key_image.url),slika)

How do I stop auto_link from escaping angle brackets?

I would like for my app to allow users to post links in their posts, and for those links to automatically be recognized. To do so, I have been using auto_link as such: the following is the partial that is called to show a person's post:
_post.html.erb:
<tr>
<td >
<span class="post_header"><h4><%= link_to "#{post.user.first_name} #{post.user.last_name}", post.user %></h4></span>
<p> <%= auto_link(post.content) %> </p>
<span class="post_timestamp">
Opined <%= time_ago_in_words(post.created_at) %> ago
</span>
</td>
</tr>
this outputs the following, for a single post.content:
<p> Wondering if this link <a href="http://www.economist.com/blogs/freeexchange">http://www.economist.com/blogs/freeexchange</a> will become a proper link
Why does auto-link create/escape the angle brackets to <a etc? Is there some way to fix this, as this does not create working links. Instead the output in the browser is:
Wondering if this link http://www.economist.com/blogs/freeexchange will become a proper link
In Rails 3, erb will default to not allow any ruby output to contain html. To get around this you can use "some string".html_safe
<%= auto_link(post.content).html_safe %>
But of course any html or javascript will then be allowed. So...
<%= sanitize(auto_link(post.content).html_safe) %>
Note that auto_link was removed with Rails 3.1.
See this answer for replacement solutions.
tybro0103's solution works, but if you want ONLY links as proper HTML, you'll need
sanitize(auto_link(post.content).html_safe,tags:'a')

Resources