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 %>
Related
I want to have a link to a certain page in my ruby on rails app and I want it to be displayed as a glyphicon ("glyphicon glyphicon-eye-open"), rather than "Show". Please help me. This is my link_to:
<%= link_to 'Show', show_schedule_in_teams_path %>
Please help me. Thanks in advance.
You can pass a block to link_to (If you'd like to use an icon tag etc).
Using FontAwesome as an example,
link_to LINK_URL do
fa_icon 'bathtub'
end
https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
this code if you want to show glyphicon using bootstrap
<%= link_to '<i class="glyphicon glyphicon-eye-open"></i>'.html_safe, show_schedule_in_teams_path, :class => 'btn btn-mini btn-danger' %>
First of all, make sure you have bootstrap installed on your rails application. To make so
Add gem 'bootstrap-sass' to your Gemfile
Execute bundle install
Add *= require bootstrap_framework to your application.css
Restart your rails application server
After that, you should be able to use the following code:
<%= link_to '<span class="glyphicon glyphicon-eye-open"></span>'.html_safe, show_schedule_in_teams_path %>
Im trying to figure out how to make a link in my show page.
I want a link to
<%= #project.external_link %>.
I want to display it as:
<%= link_to '<i class="fa fa-link"></i>' %>
I've tried at least 20 variations on combining these two and I can't find a way that works.
Does anyone know how to display a font awesome icon, with a link to a dynamic field?
Try this:
<%= link_to #project.external_link do %>
<i class="fa fa-link"></i>
<% end %>
Try using :
<i class="fa fa-link"></i>
I'm trying to have a Font Awesome icon be hyperlinked with the string of a Rails attribute.
I have tried this,
<%= link_to do %>
<i class="fa fa-link"><% school.website %></i>
<% end %>
this,
<i class="fa fa-link" href="<% school.website %>"></i>
and this:
<i class="fa fa-link" href="<%= school.website %>"></i>
among other variations, and can't seem to get the syntax right.
Would really appreciate some help with the syntax here, can't seem to find a specific answer to how linking with attributes works, only actual static text hrefs.
PS: I'm new to Rails / using Rails 4.2.
Try this:
<%= link_to ('<i class="fa fa-link"></i>').html_safe, desired_path %>
Considering school.website is a URL and you have included icons in CSS, the following will do:
<%= link_to(school.website) do %>
<i class="fa fa-link"></i>
<% end %>
Passing it as a block works too.
<%= link_to school.website do %>
<%= '<i class="fa fa-link"></i>'.html_safe %>
<% end %>
Assuming school.website is a URL that you would like to link to and display the link as well alongside the link icon from font awesome:
<%= link_to("<i class='fa fa-link'></i> #{school.website}".html_safe,school.website) %>
I am going to suggest something a little different.
I am personlly using glyphicons/fa a lot on my website, so I decided to create a little helper in application_helper.rb
application_helper.rb
def fa(glyph, text = nil)
html = "<i class=\"fa fa-#{glyph}\"></i>"
html << " #{text.to_s.html_safe}" if text
html.html_safe
end
And actually the correct syntax to use in views becomes either :
<%= link_to(school.website, class: "xxx") do %>
<%= fa('link') %>
<% end %>
OR (more compact)
<%= link_to(school.website, class: "xxx"){ fa('link') } %>
You can use a block as well if your link target is hard to fit into the name parameter. ERB example:
try this:
<%= link_to(school.website) do %>
<%= fa_icon "fas link" %>
<% end %>
I would like to convert this:
<a class="btn btn-google-plus" href="https://plus.google.com/share?url=SHAREMESSAGE" title="Share on Google+" target="_blank">
<i class="fa fa-google-plus"></i>
</a>
Into a Rails friendly link_to that not uses post.title in the URL and also includes a link to the current post.
In other words, I started by doing this in HTML like this:
<a class="btn btn-google-plus" href='https://plus.google.com/share?url=<%= "#{post.title} - Read more at #{post}" %>' title="Share on Google+" target="_blank">
<i class="fa fa-google-plus"></i>
</a>
The issue with this is that this generates a URL like this (the Twitter equivalent, but the principle is the same):
http://twitter.com/home?status=PNPYO%20saddened%20at%20passing%20of%20Roger%20Clarke%20-%20Read%20more%20at%20#<Post:0x00000101660e98>
Where it returns a Post object. The issue I ran into quickly, was that I wasn't quite sure how to generate a link_to within a link_to. Is that even possible?
This is how I want the final status on Twitter to look:
PNPYO saddened at passing of Roger Clarke - Read More at http://example.com/pnpyo-saddened-at-passing-of-roger-clarke
How do I achieve this in the most Rails-friendly way possible? I am not averse to just using regular a href tags, if it can't be done with a link_to helper. But either way, I still need to be able to generate a link_to within the status message.
You can achieve as follow :
<%= link_to "https://plus.google.com/share?url=#{post.title} - Read more at #{post}", :class => "btn btn-google-plus" :title => "Share on Google+" :target => "_blank" do %>
<i class="fa fa-google-plus"></i>
<% end %>
You don't want to call a link_to within a link_to, but you want to call an url_helper directly.
link_to in rails is a helper method which generates the necessary html code for links. Whether you want to use this convenience function or not, what you are searching for is a direct method to generate an url that you can concatenate into a string.
Simply use the following as the href for your anchor:
http://twitter.com/home?status=<%=u "#{post.title} - Read more at #{post_url(post)}" %>
(<%=u %> performs the url encoding of the string)
I've been looking everywhere for a good explanation of how to add glyphicons to rails link_to and button_to helpers, but I've found very little. What I've gathered so far has led me to this:
<li>
<%= link_to deals_path, class: "btn btn-default" do %>
<%= content_tag(:i, "Dasboard",:class=>' glyphicon, glyphicon-th-large') -%>
<% end %>
</li>
This doesn't work though and I think the one example I found was from Bootstrap 2. Can anyone point me to a good resource on this, or provide a quick example? Thanks in advance!
I found the answer to this here
The basic form of a glyph link in rails looks like this:
<%= link_to deals_path, class: "btn btn-default" do %>
<i class="glyphicon glyphicon-euro"></i> Dashboard
<% end %>
Modify as needed. The second example in that link didn't work for me, I assume because I'm using the rails_bootstrap_sass gem? Regardless, the above form worked for me.
If you're looking for an inline method, This works for me:
<%= link_to '<i class="glyphicon glyphicon-th-large"></i> Dasboard'.html_safe, deals_path, class: 'btn btn-default' %>
The <i></i> can go either side of the 'Dashboard' I've only tested this particular example out in Rails 4 with Bootstrap 3 but this was the method I used prior in Rails 3 and Bootstrap 2
Hope this helps somebody in the future
Edit: Removed comma to render the glyphicon correctly.
In my experience the answer by #settheline is almost ideal, but on my website it changes the font relative to other buttons without icons. So I ended up doing something like this:
<%= link_to deals_path, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-euro"></span> Dashboard
<% end %>
And this seems to keep the font equal to other iconless buttons.
Using slim, here's link_to:
= link_to deals_path
span.glyphicon.glyphicon-th-large
and button_to:
= button_to deals_path, class: "btn btn-primary"
span.glyphicon.glyphicon-th-large
It's possible to add more text/etc. to the button as well, just don't nest it under the glyphicon's span.
Using HAML:
= link_to deals_path, class: "btn btn-default" do
= "Dashboard"
%span.glyphicon.glyphicon-th-large
You can use the font-awesome-rails gem for this purpose, and then do:
<li><%= link_to raw(fa_icon("dashboard", class: "th-large"), deals_path, class: "btn btn-default" %>
&For those who'd avoid unnecessary repetition of the long-winded thing..
i shove something like this in my app/helpers/application_helper.rb:
module ApplicationHelper
def glyph(icon_name_postfix, hash={})
content_tag :span, nil, hash.merge(class: "glyphicon glyphicon-#{icon_name_postfix.to_s.gsub('_','-')}")
end
end
Example .erb usage:
<%= button_tag glyph("heart-empty", aria_hidden: "true", foo: "bar"), type: "button", class: "btn btn-default" %>
<%= link_to glyph(:eye_open) + " Oook", some_path, class: "nav" %>
I am using this in Rails4 but i think it might also work in Rails3
Ooook! i also happened to notice this advise from the bootstrap (Currently v3.3.5) docos:
Don't mix with other components Icon classes cannot be directly combined with other components. They should not be used along with
other classes on the same element. Instead, add a nested <span> and
apply the icon classes to the <span>.
Only for use on empty elements Icon classes should only be used on elements that contain no text content and have no child elements.
There is faster and easier way to apply (fontawasome) icons without additional gem installations.
You may follow this pattern:
<%= link_to root_path, class: "nav-link" do %>
<i class="fas fa-pencil-alt"></i>
<% end %>
Of course, you must first create a kit FREE account from the FONTAWASOME and it must be set in your application.html.erb's head to use the icons.
Follow the instructions given here to create an account in Fontawasome (if you don't have one yet).
If you need an example, you can check out my repo in GitHub