How to add a facebook glyphicon to rails link_to - ruby-on-rails

I am trying to add a glyph-icon to my Facebook link_to in ruby on rails but when I try it doesn't show up any idea on how to do this?
This is the what I have attempted:
<li>
<i class= "fa fa-facebook"></i>
<%= link_to "Facebook", '/facebook' %>
</li>
any help is greatly appreciated..thank you

<%= link_to '/facebook' do %>
<i class= "fa fa-facebook"></i>
<% end %>

what about:
<li>
<%= link_to '/facebook' do %>
Facebook // delete if you don't need text
<i class= "fa fa-facebook"></i>
<% end %>
</li>

Related

Dropdown menu not working on heroku after deploy

I have a simple drop down menu which works great locally but when I deploy my application to Heroku, it doesn't work. It doesn't show menu options. So, when I click on Account, nothing happens.
I have added this in my production.rb file
config.assets.compile = true
config.serve_static_assets = true
config.assets.digest = true
Tired this before pushing code to Heroku, But still it not working for me
RAILS_ENV=production bundle exec rake assets:precompile
git add public/assets
git commit -m "compiled assets"
git push heroku master
Here is my code:
<% if user_signed_in? %>
<li class="dropdown">
<a class="dropdown-toggle count-info" data-toggle="dropdown" href="#" aria-expanded="false">
My Account <i class="caret"></i>
</a>
<ul class="dropdown-menu dropdown-settings">
<li>
<%= link_to user_path(current_user) do %>
<i class="fa fa-user"></i> Profile
<% end %>
</li>
<li>
<%= link_to edit_user_path(current_user) do %>
<i class="fa fa-pencil"></i> Edit Profile
<% end %>
</li>
<li>
<%= link_to edit_user_registration_path do %>
<i class="fa fa-unlock"></i> Change Password
<% end %>
</li>
<li class="divider"></li>
<li>
<%= link_to destroy_user_session_path, method: :delete do %>
<i class="fa fa-sign-out"></i> Log out
<% end %>
</li>
</ul>
</li>
<li>
<div class="m-t-sm">
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'fa fa-sign-out'%>
</div>
</li>
<% else %>
<li>
<div class="m-t-sm">
<%= link_to "Sign in", new_user_session_path, :class => 'btn btn-link btn-md'%> or
<%= link_to "Sign up", new_user_registration_path, :class => 'btn btn-md btn-success btn-rounded m-l'%>
</div>
</li>
<% end %>
I have solved this issue by removing this code from application.html.erb file:
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

Nested html with content_tag

I want to get this html using content_tag or content_tag_for:
<a href="javascript:;" class="nav-link ">
<%= fa_icon 'book', 'aria-hidden': true %>
<span class="title">Courses</span>
</a>
I could put fa_icon inside link_to, but don't know how to put span. And if it is not doable, what if i do this way:
<a href="<%=courses_path%>" class="nav-link ">
<%= fa_icon 'book', 'aria-hidden': true %>
<span class="title">Courses</span>
</a>
Why not the following?:
<%= link_to "javascript:;", class: "nav-link" do %>
<%= fa_icon 'book', 'aria-hidden': true %>
<%= content_tag :span, "Courses", class: "title" %>
<% end %>
I would try link_to ... do. Because it's a standard approach that's more descriptive than content_tag I think it will be easier to understand the original intent of the code when you come back 2 years from now to do maintenance.
Here's what your snippet might look like, to get you going:
<%= link_to courses_path, class: 'nav-link' do %>
<%= fa_icon 'book', 'aria-hidden': true %>
<span class="title">Courses</span>
<% end %>
Here is the documentation for Rails 4.2: http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to

How to Render Current User into Nav-Bar Button

Running:
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
Rails 4.2.5
I want to render the current user name into the nav-bar button using the dropdown-menu class. I would like to replace "Account Info" with "Hi "current_user".
<li class="dropdown">
Account Info <span class="caret"></span>
<ul class="dropdown-menu">
<li> <%= link_to "Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li>
<li> <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li>
</ul>
<ul class = "nav pull-right">
</ul>
<% else %>
<!-- <%= link_to "Sign up", new_user_registration_path, class: "fa fa-sign-in" %> -->
<li><%= link_to "Sign in", new_user_session_path, class: "fa fa-sign-in" %></li>
<% end %>
</ul>
I'm using devise (3.5.3) for user authentication.
You'll just need to use an if statement to work out if the user is signed_in (I presume this method exists in Devise) and show a different button if the user is signed in:
<li class="dropdown">
<% if signed_in? %>
Hi, <%= current_user.user_name %> <span class="caret"></span>
<% else %>
Account Info <span class="caret"></span>
<% end %>
<ul class="dropdown-menu">
<li> <%= link_to "Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li>
<li> <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li>
</ul>
<ul class = "nav pull-right">
</ul>
<% else %>
<!-- <%= link_to "Sign up", new_user_registration_path, class: "fa fa-sign-in" %> -->
<li><%= link_to "Sign in", new_user_session_path, class: "fa fa-sign-in" %></li>
<% end %>
</ul>
You'll probably want to use a similar if, else, end statement to hide the sign_in/sign_up buttons when the current_user is signed_in too - as they won't want to see that.
This would suffice in your design simply.
<%- if user_signed_in? %>
<%= current_user.user_name %>
<% end %>
You have to first ask if the user exists. The reason why is because if you run .user_name on a user who is not logged in it is essentially running a method on a nil value. Which is a big no no.
Now if you still have an issue you have to make sure you have this in your controller:
before_filter :authenticate_user!
<%= content_tag :li, class: "dropdown" %>
<% if user_signed_in? %>
<%= link_to "Hi, #{current_user}", "#", data: {toggle: "dropdown"}, role: :button, aria: {haspopup: "true", expanded: "false"} %>
<%= content_tag :ul, class: "dropdown-menu" do %>
<%= content_tag :li, link_to("Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o") %>
<%= content_tag :li, link_to("Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out") %>
<% end %>
<% else %>
<%= content_tag :li, link_to("Sign in", new_user_session_path, class: "fa fa-sign-in") %>
<% end %>
<% end %>
Refs:
content_tag
With some testing <%= current_user.first_name %> in Hi, <%= current_user.first_name %> <span class="caret"></span> seem to give me the outcome I was looking for. Thanks for your help and pointing me in the right direction. #Alain-Goldman.

How do I generate a <button> tag with an <i> inside?

I want to produce this HTML:
<a class="action-btn btn-delete" href="/product/9">
<i class="icon-trash icon-white"></i>
</a>
But I read that I need to use the button_to helper method, rather than link_to.
So I tried this:
<%= button_to product, method: :delete, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
But that doesn't work.
Thoughts?
I think you can use the link_to:
<%= link_to product, :method => "DELETE", :class => "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
I don't see how you can't use [link_to][1]
<%= link_to product, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
Try, it may work,
<%= link_to product, method: :delete, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i> Delete
<% end %>

how to convert this ruby on rails code to include html as well

How can I take this..
<li>
<%= link_to image_tag(product.photo.url(:small)), product_path(product.id, :forum_id => params[:forum_id]) %>
<% if params[:forum_id] %>
<%= link_to "Add", addtoforum_user_path(:products => [product.id],:forum_id => params[:forum_id], tp: "add") %>
</li>
to be like this.. (right now it is only inlcluding the image)
<li>
<a href="">
<figure>
<img src="image-url....">
</figure>
<span>product name</span>
</a>
</li>
Thanks
Not sure I understand the question but I guess the issue is to output the figure, img tags etc inside the a tag?
This can be achieved by passing a block to the link_to method, like this:
<li>
<%= link_to product_path(product.id, :forum_id => params[:forum_id]) do %>
<figure>
<%= image_tag(product.photo.url(:small)) %>
</figure>
<span><%= product.name %></span>
<% end %>
</li>

Resources