The best way to convert current_page to an action helper - ruby-on-rails

I have a dropdown that that would be active when page is on using the current_page? method like so:
<ul>
<li class="dropdown">
<%= #company.name %> <b class="caret"></b>
<ul class="dropdown-menu">
<li><%= link_to 'Dropdown A', company_dropA_path(#company) %></li>
<li><%= link_to 'Dropdown B', company_dropB_path(#company) %></li>
<li><%= link_to 'Dropdown C', company_dropC_path(#company) %></li>
<li><%= link_to 'Dropdown D', company_dropD_path(#company) %></li>
</ul>
</li>
</ul>
As you may noticed, the current_page gets really long and messy. How could I convert this to a helper?

%w[A B C D].any?{|c| current_page?(send("company_drop#{c}_path", #company))}

Related

bootstrap rails drop down

I'm having difficulty trying to get a dropdown box in rails using the bootstrap framework.
<li class="dropdown">
Categories<span class="caret"></span>
<ul class="dropdown-menu">
<!-- trying to insert dynamic list for drop down -->
<li role="separator" class="divider"></li>
<li><%= link_to "All Categories", categories_path %></li>
</ul>
</li>
I figured it would be similiar to the <ul> list </ul> from index
<% #categories.each do |category| %>
<li><%= category.name %></li>
<%end >
But the rails says .each is not defined... my dropdown is in a _partial html file. What am I doing wrong?
Thanks,
Joe
When you call your partial, you should pass locals like that:
<%= render 'my_partial', catgories: #categories %>
And in your partial:
<% categories.each do |category|%>
<li><%= category.name %></li>
<% end %>

Syntax error, unexpected keyword_ensure, expecting keyword_end

I currently have 3 loops im in my _header I have
if current_user.admin? && !current_user?(user)
Also
<% if logged_in? %>
And
<% else %>
It all works fine with no error if i take out
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
When I paste it back in i get the error. I dont know whats wrong with this syntax.
Can anyone help?
<ul class="nav navbar-nav navbar-right">
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
<% if logged_in? %>
<li><%= link_to "Dash", users_path %></li>
<ul class="nav navbar-nav">
<li class="dropdown">
Items<span class="caret"></span>
<ul class="dropdown-menu">
<li><%= link_to "New Item", new_item_path %></li>
<li><%= link_to "Edit Items", user_items_path %></li>
<li>Mass Upload Items</li>
<li>Upload Item Images</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav">
<li class="dropdown">
Profile<span class="caret"></span>
<ul class="dropdown-menu">
<li><%= link_to "View Profile", user_path(current_user) %></li>
<li><%= link_to "Edit Profile", edit_user_path(current_user) %></li>
</ul>
</li>
</ul>
<li><%= link_to "Messages", users_path %></li>
<li class="dropdown">
Settings<span class="caret"></span>
<ul class="dropdown-menu">
<li>Edit Money Back Policies</li>
<li>Edit Warranty Policies</li>
</ul>
</li>
<li><%= link_to "Log out", logout_path, method: "delete" %></li>
<% else %>
<li><%= link_to "Log In", login_path %></li>
<li>Sign Up</li>
<% end %>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</header>
You're not properly closing the if you pasted :
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
You should try to replace that by :
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
<% end %>
structure of if loop in ruby.your end tag of if loop is incorrect.see below
<%if%>
//condition here
<%end%>
can you try this out:
<% if logged_in? && !current_user?(user) && current_user.admin? %>
<li><%= link_to "Admin", users_path %></li>
</end>
Syntex error in closing of if block syntex.
1 <% if current_user.admin? && !current_user?(user) %>
2 <li><%= link_to "Admin", users_path %></li>
3 </end>
Error at line 3, replace line 3 ie </end> with <% end %>

unable to correctly load font-awsome icons in navbar using link_to method in rails

Im trying to add font-awsome icons to my (link_to) links within a simple navbar i created, however im not sure as to how to correctly write the code using ruby syntax as apposed to the html. This is the code I would like to add the icons to:
<nav>
<ul>
<li><%= link_to "Main", root_path %></li>
<li><%= link_to "Employee Profile", profile_path %>
<ul>
<li><%= link_to "Tasks / Activities", "#" %></li>
<li><%= link_to "Vacations", "#" %></li>
</ul>
</li>
<li><%= link_to "Projects", projects_path %></li>
<li><%= link_to "Projects Documents", project_docs_path %></li>
<li><%= link_to "Search", search_path %></li>
<li><%= link_to "Reports", reports_path %></li>
<li><%= link_to "Feedback", feedback_path %></li>
</ul>
I would like each one of my (link_to) tags to have its own font-awsome icon. As an exmample, could you provide me with the correct syntax to include the following icon to my "Employee Profile" section:
<i class="fa fa-user"></i>
Here is a link to the icon's page:
http://fortawesome.github.io/Font-Awesome/icon/user/
Thanks!
Link_to tag can be used as a 'block'. You can use in this way to include font-awesome in link_to
<%= link_to profile_path do %>
<i class="fa fa-user"></i>Employee Profile
<% end %>

Showing users name on nav bar when logged in using ruby on rails

I want to be able to replace Accounts tab on my navigation bar with the User's name when they logged in. How would i go about doing that. Would i have to use some sort of embedded ruby like <%= #user.name %>
heres the html
enter code here
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", user_path(current_user) %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: 'delete' %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
Try:
<%= current_user.name %>

_header.html.erb account dropdown function does not work

I am following the ruby on rails tutorials from http://ruby.railstutorial.org .
The following is the _header.html.erb code, is the same as the tutorials. but the drop down function of account does not work.
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", users_path %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
Without seeing more of the application it's hard to determine the exact problem, however based on what you have supplied I can suggest the following:
refer to http://twitter.github.com/bootstrap/javascript.html#dropdowns and ensure you are using the right markup nesting (looks like you are)
make sure you have included the javascript files within your application.js file (your manifest file). The docs for bootstrap here: http://twitter.github.com/bootstrap/components.html#navbar say that you need to have the dropdown JS file included to make the dropdown work properly.
Please let me know if any of this helps.

Resources