bootstrap rails drop down - ruby-on-rails

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 %>

Related

How to iterate through my array within a hash

Firstly, I am hoping my understanding is correct and I do in fact have a an array within my hash.
I've gotten so far but cant seem to iterate through each hash and its array. What I would like to do is have each tournament grouped by date (year only) and have all its tournaments listed underneath.
So far my logic is
Controller
#tournaments = Tournament.all
#tournament_year = #tournaments.group_by {|y| y.tourn_date.year}
View
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<li><%= y %></li>
<% end %>
</ul>
hash
{2014=>[#<Tournament id: 3, name: "WTS ", tourn_date: "2014-04-26", tourn_location: "Cardiff", created_at: "2014-04-26 14:57:21", updated_at: "2014-04-26 14:57:21">, #<Tournament id: 4, name: "CTS Nottingham", tourn_date: "2014-05-26", tourn_location: "Nottingham", created_at: "2014-04-26 14:57:39", updated_at: "2014-04-26 14:57:48">]}
My desired output would be below
<h3>2014</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
<h3>2013</h3>
<ul>
<li>Tournament Name 1</li>
<li>Tournament Name 2</li>
</ul>
Change your view as below:
<ul class="resp-tabs-list">
<% #tournament_year.each do |y, t| %>
<h3><%= y %></h3>
<ul>
<% t.each_with_index do |tournament, i| %>
<li><%= tournament.name %> <%= i %></li>
<% end %>
</ul>
<% end %>
</ul>
I would suggest
<% #tournament_year.each do |y, t| %>
<h3> <%= y %></h3>
<ul class="resp-tabs-list">
<% t.each do |tournament| %>
<li><%= "#{tournament.to_s}" %></li>
<% end %>
</ul>
<% end %>
where
#app/models/tournament.rb
def to_s
"#{name} #{id}"
end
Ive just come up with this solution as an example, seems to work, can anyone see any reason why this would be incorrect?
<ul class="resp-tabs-list">
<%= #tournament_year.each do |y, t| %>
<li><%= y %></li>
<%= #tournament_year[y].each do |x| %>
<li><%= x.name %></li>
<% end %>
<% end %>
</ul>

Iterating through controller methods in view

I am trying to refactor some of the code in my view
<% if controller.controller_name == "overview" %>
<div id="left-menu">
<ul>
<li>Office</li>
<hr>
<li><%= link_to "Overview", root_path %></li>
<li><%= link_to "Personnel", personnel_path %></li>
<li><%= link_to "Results", results_path %></li>
<li><%= link_to "Statistics", statistics_path %></li>
</ul>
<ul>
<li>Economy</li>
<hr>
<li>Finances</li>
<li>Contracts</li>
<li>Transfers</li>
<li>Sponsors</li>
</ul>
</div>
<% elsif controller.controller_name == "market" %>
<div id="left-menu">
<ul>
<li>Items</li>
<hr>
<li><%= link_to "Engines", market_engines_path %></li>
<li><%= link_to "Weapons", market_weapons_path %></li>
<li><%= link_to "Armor", market_armor_path %></li>
<li><%= link_to "Chips", market_chips_path %></li>
</ul>
<ul>
<li>Personnel</li>
<hr>
<li><%= link_to "Drivers", market_drivers_path %></li>
<li><%= link_to "Servicemen" %></li>
<li><%= link_to "Programmers" %></li>
<li><%= link_to "Managers" %></li>
</ul>
</div>
<% end %>
Where each <li> corresponds to a method in the controller. I would like to be able to add new methods to my controllers, and then have them dynamically inserted in the view. So is there a way to iterate over the methods in a controller?
As per the answer provided in the referenced question, Controller.action_methods seems to be the method you need, however, to integrate into your code is as follows:
<% lists = %w(items personell) %>
<% for list in lists do %>
<ul>
<li><%= list.titleize %></li>
<hr>
<% items = MarketsController.action_methods %>
<% for item in items do %>
<li><%= link_to item.titleize, eval("market_#{item}_path") %></li>
<% end %>
</ul>
<% end %>

The best way to convert current_page to an action helper

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))}

_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.

ruby "first" helper method?

i have the following file member.html.erb:
<ul class="members-list">
<%#members.each do |member|%>
<li class="member-item">
<%=member.name%>
</li>
<%end%>
</ul>
i want to add the "first" class to the first li that will be generated (just to the first), so it would look like this:
<ul>
<li class="member-item first">john</li>
<li class="member-item">chris</li>
</ul>
any thoughts?
You could acheive this using CSS aswell if its purely for the styling. If you either has a ID or class on the ul then you could in CSS write
ul.member-list li:first-child {
WHATEVER STYLING CODE THAT YOU WOULD PUT IN member-item first
}
and for the rest of the li-tags
ul.member-list li {
}
If you want the class to be added you could e.g
<ul>
<% #members.each_with_index do |member, i| %>
<% if i == 0 %>
<li class="member-item first">
<% member.name %>
</li>
<% else %>
<li class="member-item">
<% member.name %>
</li>
<% end %>
</ul>
Similar, but more terse, approach to Andreas/Heikki's answer:
<ul>
<% #members.each_with_index do |member, i| %>
<li class="member-item<%= ' first' if i == 0 %>">
<% member.name %>
</li>
<% end %>
</ul>

Resources