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>
Related
How can I display the post base in the current category, I can show the category but only one post show.
In my controller:
#area_category = Project.joins(:area_category).group(:area_category_id)
In my view
<% #area_category.each do |category| %>
<%= category.area_category_name %>
<% category.project_title %>
<% end %>
Basically, i want to display category and post on each category
Something like this:
<ul>
<li>Category 1
<ul>
<li>Post 1</li>
<li>Post 2</li>
</ul>
</li>
<li>Category 2
<ul>
<li>Post 1</li>
<li>Post 2</li>
</ul>
</li>
</ul>
controller
#area_category = AreaCategory.preload(:projects)
view
<% #area_category.each do |category| %>
<%= category.area_category_name %>
<% category.projects.each do |project| %>
<%= project.project_title %>
<% end %>
<% end %>
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 %>
In my view I do this:
<ul class="list-group">
<%= JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
It lists everything out like it should, but then at the end it returns the value of current_printer.stripe_managed_account.fields_needed. I'm not sure why it does this, or how to prevent it from doing this.
This is a screenshot:
Replace <%= %>, on your each line by <% %>.
Because <%= #your each %> is same that : <% puts #your each %>.
Try this:
<ul class="list-group">
<% JSON.parse(current_printer.stripe_managed_account.fields_needed).each do |f| %>
<li class="list-group-item">
<%= f.gsub!(/[._]/, ' ') %>
</li>
<% end %>
</ul>
Thanks #vee
I have a number of steps and I have a counter that I would like to increment as the block iterates through the steps. More specifically, if its media type is "excel" or anything else I would like it to say step 2, step 3, step 4 etc. However in the view it only says step 1, step 1, step 1 - it does not properly increment. What is wrong? Any help would be greatly appreciated.
<% #step_list.each do |i| %>
<% x = 0 %>
<% case %>
<% when i.media_type == 'video' %>
<% x += 1 %>
<li class="active">
<span>Video</span>
</a>
</li>
<% when i.media_type == 'excel' %>
<% x += 1 %>
<li class="">
<span>Step <%= x %> </span>
</li>
<% else %>
<% x += 1 %>
<li class="dark-nav ">
<span>Step <%= x %></span>
</li>
<% end %>
<% end %>
The problem in your code is that you initialize the variable x in each loop, you should initialize it to zero before your loop.
To solve this, You can refactor this with the method .each_with_index do |element, i|:
<% #step_list.each_with_index do |step, i| %>
<% case step.media_type %>
<% when 'video' %>
<li class="active">
<span>Video</span>
</li>
<% when 'excel' %>
<li class="">
<span>Step <%= i %></span>
</li>
<% else %>
<li class="dark-nav ">
<span>Step <%= i %></span>
</li>
<% end %>
<% end %>
Why don't you use each_with_index ?
[1, 2, 3].each_with_index do |n, i|
end
You reset your x each time, try that :
<% #step_list.each_with_index do |i,index| %>
<% case i.media_type %>
<% when 'video' %>
<li class="active">
<span>Video</span>
</a>
</li>
<% when 'excel' %>
<li class="">
<span>Step <%= index %> </span>
</li>
<% else %>
<li class="dark-nav ">
<span>Step <%= index %></span>
</li>
<% end %>
<% end %>
<% end %>
I am trying to make a simpel treelistmenu with ancestry.
Here is my code in view and it is not working:
<ul>
<% for cat_opg in CatOpg.roots %>
<li> <%= cat_opg.navn %><li>
<% for cat_opg in CatOpg.children %>
<li> <%= cat_opg.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
And my controller:
def liste
#cat_opg = CatOpg.find(:all)
end
I want to make this simpel tree menu as:
Root
-children
Root
-children
I dont know what i am doing wrong.
PS: I am a rails beginner
First of all, you are getting to model in view, not to local variable.
Second, you're rewriting variable.
It should be something like this:
<ul>
<% cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %><li>
<% cat_opg_root.children each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %><li>
</ul>
<% end %>
</ul>
<% end %>
Alex thank you for your answer.
Now it works
Controller: #cat_opg = CatOpg
And the view:
<ul>
<% #cat_opg.roots.each do |cat_opg_root| %>
<li> <%= cat_opg_root.navn %></li>
<% unless cat_opg_root.children.empty? %>
<ul>
<% cat_opg_root.children.each do |cat_opg_child| %>
<li> <%= cat_opg_child.navn %></li>
<% end %>
</ul>
<% end %>
<% end %>
</ul>