How can I go about embedding font-awesome icons into my Ruby <%= link_to code?
The below does NOT work, is it possible to accomplish the below some how so that it actually works?
<li><%= link_to "<span class="fa fa-minus-circle fa-1x"></span> Settings</span>", destroy_user_session_path, :method => :delete %></li>
Thanks!
Johnson
have you try this:
<li><%= link_to your_path do %><span class="fa fa-minus-circle fa-1x"></span> Settings <% end %></li>
for instance with font-awesome in some of my codes:
<li><%= link_to edit_contact_path(#contact) do %><i class="fa fa-pencil-square-o"></i> Edit<% end %></li>
<li><%= link_to #contact, method: :delete, data:{confirm: "Delete this contact?"} do %><i class="fa fa-exclamation-triangle"></i> Delete<% end %></li>
Here is the documentation, see the section with link_to ... do .... end
link to documentation
I've always done it including the class in the Ruby code like this,
<li><%= link_to " Sign Up", '#', class: "fa fa-user-plus" %></li>
This includes the icon on the left of the text so you have to include the space preceding "Sign up" in order to get it to look right.
Better option for you is use this gem:
https://github.com/h4b00/awesome_link
After installation you can simply use(for example):
<%= awesome_link('fa-pencil-square-o', root_path, method: :update) %>
Related
I have the current code in my traders.index.html file
<ul>
<% #traders.each do |trader| %>
<li><%= link_to trader.name, trader %></li>
<%end%>
</ul>
I want to add an extra parameter to be sent through, I tried
<li><%= link_to trader.name, trader, {:restricted => params[:s]} %></li>
But this doesn't send the parameter, whats the actual format of the link_to to get this done?
You can do:
<%= link_to trader.name, trader_path(trader, restricted: params[:s]) %>
I have this link
<li><a href="<%= destroy_user_session_path(:method => :delete) %>"><button
class="button icon-left ion-ios7-compose">Log out</button></a></li>
But it is not working. How do I fix the :method => :delete so that it works in the link?
I am getting this error:
No route matches [GET] "/users/sign_out"
Also, I am using devise for users.
use the link_to helper (also check out your html, a button isn't usually found inside a link).
<li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li>
You have to write it like this
<li><%= link_to "Log Out", destroy_user_session_path, :class => "button icon-left ion-ios7-compose", :method => :delete %></li>
Instead of hard coding a link description, I would like to use some Ruby code.
This is the original:
<li><%= link_to "Open Projects List", workorders_index2_path %></li>
This didn't work for me:
<li><%= link_to "<%= current_tenant.name_workorder.capitalize.pluralize %>", workorders_index2_path %></li>
Thanks for the help!
You don't need to use quotes at all:
<li><%= link_to current_tenant.name_workorder.capitalize.pluralize, workorders_index2_path %></li>
You already ARE using ruby code. <%= %> everything inside of that is pure ruby. link_to is a ruby method, and "Open Projects List" is the first parameter to that method, it's a string. Anything you can do in ruby you can send here - don't send a string, send the variable:
<li><%= link_to current_tenant.name_workorder.to_s.capitalize.pluralize, workorders_index2_path %></li>
You can also use string interpolation like you would with regular ruby:
<li><%= link_to "Open Project #{current_tenant.name_workorder}", workorders_index2_path %></li>
So in my application.html.erb I have my navigational structure that looks something like this:
<div id="navigation">
<ul class="pills">
<% if current_page?(:controller => 'welcome', :action => 'index') %>
<li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
<li><%= link_to "Settings", settings_path %></li>
<li><%= link_to "Sign Out", signout_path %></li>
<% elsif !current_page?(:controller => 'welcome', :action => 'index') %>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
<li><%= link_to "Settings", settings_path %></li>
<li><%= link_to "Sign Out", signout_path %></li>
<% end %>
</ul>
</div>
However, what I would like to do, is once they are on any of the pages in the navigation, it applies a class active to the respective link.
So for instance, if the user is on mydomain.com/johnbrown which is the Profile link, the rails helper would look something like this:
link_to "Profile", vanity_path(:vname => current_user.username), :class => "active".
But how do I do that in a programmatic way, so I am not duplicating content? i.e. how do I get that functionality for all the pages in my navigation and write it as DRY as possible?
Thanks.
This is a really great question. I've never really been happy with the solutions I've seen or come up with. Maybe we can get one together here.
Here is what I've tried in the past
I've made a helper that returns a hash with :class defined since I use HAML
def active_tab(path)
request.path.match(/^#{path}/) ? { :class => 'active' } : {}
end
ex usage:
= link_to "Dashboard", dashboard_path, active_tab("#{dashboard_path}$")
Or an alternative along the same lines
def active_class(path)
request.path =~ /#{path}/ ? 'active' : nil
end
ex usage:
= link_to 'Presentations', admin_presentations_path, :class => "#{active_class('presentations')}"
I would love to see some other suggestions on this.
I found this answer for a bootstrap related navbar but you could easily use it with your nav.
Answer taken from here
You can use helper for handle "current_page?", example a method :
module ApplicationHelper
def is_active?(link_path)
if current_page?(link_path)
"active"
else
""
end
end
end
example bootstrap navbar
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul class="nav">
<li class="active">Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
</div>
So, on view looks like
<li class="<%= is_active?(some_path) %>">
<%= link_to "name path", some_path %>
</li>
For Haml
Just simple looks like :
%ul.nav
%li{class: current_page?(some_path) && 'active'}
= link_to "About Us", some_path
You may define a helper method in application_helper.rb
def create_link(text, path)
class_name = current_page?(path) ? 'my_class' : ''
content_tag(:li, class: class_name) do
link_to text, path
end
end
Now you can use like:
create_link 'xyz', any_path which would render as
<li class="my_class">
xyz
</li>
Hope it helps!
Why don't you just take the redundant parts out of the if else block? Also take a look at 'link_to_unless'
<div id="navigation">
<ul class="pills">
<li><%= link_to_unless(current_page?(:controller => 'welcome', :action => 'index'), "Home", root_path %></li>
<li><%= link_to "Profile", vanity_path(:vname => current_user.username) %></li>
<li><%= link_to "Settings", settings_path %></li>
<li><%= link_to "Sign Out", signout_path %></li>
</ul>
</div>
then I would add some jQuery to inject active class into the link that matches the window.location pattern.
Right now I have a navigation partial that looks like this (x10 buttons)...
<% if current_controller == "territories" %>
<li><%= link_to "Territories", {:controller => 'territories'}, :class => 'active' %></li>
<% else %>
<li><%= link_to "Territories", {:controller => 'territories'} %></li>
<% end %>
<% if current_controller == "contacts" %>
<li><%= link_to "Contacts", {:controller => 'Contacts'}, :class => 'active' %></li>
<% else %>
<li><%= link_to "Contacts", {:controller => 'Contacts'} %></li>
<% end %>
Is there a more elegant/DRY solution for doing this?
In a similar vein to what Chuck said:
<% TARGETS.each do |target| %>
<li>
<%= link_to target.humanize,
{ :controller => target },
class => ('active' if current_controller == target)) %>
</li>
<% end %>
It's pretty easy to see where the repetition is in there. It's all of the general form:
<% if current_controller == XXXXX %>
<li><%= link_to XXXXX, {:controller => XXXXX}, CLASS %></li>
<% else %>
[do the same stuff minus ":class => 'active'"]
<% end %>
So we want XXXXX and CLASS to be variables (since those are the only things that change) and the rest can be a simple template.
So, we could do something like this:
%w(Contacts Territories).each |place|
<% class_hash = current_controller == place ? {:class => 'active'} : {}
<li><%= link_to place, {:controller => place}, class_hash)</li>
Check out rails-widgets on github. It provides a ton of convenience helpers for rails UI stuff (tabnavs, tooltips, tableizers, show hide toggle, simple css progressbar) in addition to navigation.
Here are the docs
Check out link_to_unless_current. Not exactly what you asked for, but it's close.
Also, you could put this kind of logic in a helper to abstract it out of the view.
Check out the simple-navigation plugin. It's an 'easy to use' rails plugin for creating navigations for your rails apps.
A slightly different version w/ link_to_unless_current:
<ul>
<% links.each do |link| -%>
<li><%= link_to_unless_current link.humanize, { :controller => target } %></li>
<% end -%>
</ul>
A good resource for stuff like this are the rails docs.