Cycle categories with subcategories - asp.net-mvc

1) i have 2 tabs Categories and SubCategories on db with relation 1 to many (im using entity framework)
2) i have to create a vertical menu like this
<ul>
<li>category 1
<ul>
<li>subcategory 1</li>
<li>subcategory 2</li>
<li>subcategory 3</li>
</ul>
</li>
</ul>
i think that my problem is here in my function
Function List_category_subcategory() As List(Of WHAT HERE???????)
Using db As New DBTestEntities
Dim q = From cat In db.categories Join subcat In db.subcategories On cat.CategoryID Equals subcat.CategoryID _
Select New With {cat.CategoryName, subcat.SubCategoryName}
List_category_subcategory = WHAT HERE???????
End Using
End Function
because i dont know what function have to return (maybe a list collection)
After it on my view have to cycle everything
something like that
<ul>
<% For Each cat In ??????%>
<li><%=Html.Encode(cat.CategoryName)%>
<ul>
<% For Each subcat In ???????%>
<li><%=Html.Encode(subcat.SubCategoryName)%></li>
<% Next%>
</ul>
</li>
<% Next%>
</ul>

Shouldn't SubCategory be a property of category (it kind of seems more natural but I could be wrong here)? Also you have a malformed li tag in the nested loop (<li<%=):
<ul id="menu-1" class="menu">
<% For Each category In Model.Category %>
<li>
<%= Html.Encode(category.CategoryName) %>
<ul>
<% For Each subcategory In category.SubCategory %>
<li>
<%= Html.Encode(subcategory.SubCategoryName) %>
</li>
<% Next %>
</ul>
</li>
<% Next %>
</ul>

Missing closing > on inner li?

Related

Rails creating unordered list in HAML

I am sorry if this question is not appropriate but I am trying to render an unordered list in HAML, HTML version looks like this:
<% #movies.comments.each do |comment| %>
<ul class="comments">
<li><%= comment.text %></li>
</ul>
<% end %>
I used 3 different html->haml online converters and all render this haml.
- #movies.comments.each do |comment|
%ul.comments
%li= comment.text
However the output is this (with 3 comments in a movie):
<ul class="comments">
<li>asdasdasd</li>
</ul>
<ul class="comments">
<li>asdasdasd</li>
</ul>
<ul class="comments">
<li>asdasdasd</li>
</ul>
Why does this create 3 separate ul elements, instead of one and 3 li elements in it?
It's not about HAML. It does what it does because you create ul element inside loop. What you want to do is to put root ul node on top:
%ul.comments
- #movies.comments.each do |comment|
%li= comment.text

Show/hide model contents in the context of other models

so I have a Category model with a "has many" relationship to my Soup model.
Currently, I have my page rendering a list of Categories with the Soups within each below. The page output looks like this:
Ramen
*Soup 1
*Soup 2
Other Soups
*Soup 3
*Soup 4
I added the ability to click on the Category name to show/hide the Soups. But I'd like to have this functionality's scope limited to each Category. In other words, I'd like to have clicking "Ramen" show/hide Soup 1 and Soup 2 only. Right now, clicking any Category shows/hides all 4 Soups.
views> categories> index.html.erb
<ul id="folderList">
<% #categories.each do |category| %>
<li>
<img src="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/folder-blue-128.png" alt="folder" width="10%">
<%= link_to category.name, '#', id: 'show_catcontents' %> (<%= category.soups.count%>)
<div id="catcontents">
<ul>
<%- category.soups.each do |soup| %>
<li><%= soup.name %></li>
<%- end %>
</ul>
</div>
</li>
<% end %>
</ul>
<script>
$(function() {
$('a#show_catcontents').click(function(event){
event.preventDefault();
$('div#catcontents').toggle();
});
});
</script>
Any and all help is appreciated
An easy way to do this while not changing all that much is to add an id to each of your elements to further specify them in your embedded ruby, and then hide that specific element rather than the whole catcontents <div> in your Javascript hide function.
For example:
<div id="catcontents">
<ul>
<%- category.soups.each do |soup| %>
<li id= <%= soup.name %> ><%= soup.name %></li>
<%- end %>
</ul>
</div>
And then of course just change your Javascript hide function to hide by the <li> id rather than the catcontents <div>.

Ruby on rails - Changing hyperlinks to table columns

I've made a list with hyper links to columns in a table called "Categories", there are 5 categories and the first set of code below makes hyperlinks to all 5.
I want to make a dropdown menu, but only show two of the categories, instead of all 5. Currently, i'm just using a href to the url, but is there some other way I can link to two columns in the "Categories" table?
Links:
<ul>
<% Category.all.each do |category| %>
<li><%= link_to category.name, items_path(category: category.name) %></li>
<% end %>
</ul>
Dropdown Menu:
<div class="container">
<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Canon
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>Canon Cameras</li>
<li>Canon Lenses</li>
</ul>
</div>
</div>
Use take() or limit() methods to limit how many items you want to get:
<ul>
<% Category.take(2).each do |category| %>
<li><%= link_to category.name, items_path(category: category.name) %></li>
<% end %>
</ul>
With respect to your question "can link to two columns in the "Categories" table?" - Please find my comment as:
SQL Approach:
SELECT CONCAT('', "name", "title") AS "name" FROM categories;
Rails Approach: <%= link_to "#{category.name} #{category.title}", items_path(category: category.name) %>

change SQL limit

This comes up when I try to load from an array in my Terminal:
Site Load (0.1ms) SELECT "sites".* FROM "sites" WHERE "sites"."user_id" = ? LIMIT 1
I want to change the limit from 1 so that I can show multiple things in my ERB file.
This is how I'm trying to show them in my ERB file.
<% if #sites.is_a? Array %>
<% #sites.take(8).each do |site| %>
<!-- list to make things inline -->
<ul class="site_tab">
<li>
<%=site.text%>
</li>
<li>
<a href="/delete_site?site_id=<%= site.id %>">
<img class="delete_button" src="https://cdn4.iconfinder.com/data/icons/geomicons/32/672366-x-128.png"/>
</a>
</li>
</ul>
<% end %>
<% elsif #sites.is_a? Site %>
<% #sites.take(8).each do |site| %>
<!-- list to make things inline -->
<ul class="site_tab">
<li>
<%= site.text %>
</li>
<li>
<a href="/delete_site?site_id=<%= site.id %>">
<img class="delete_button" src="https://cdn4.iconfinder.com/data/icons/geomicons/32/672366-x-128.png"/>
</a>
</li>
</ul>
<%end%>
<% else %>
<h2>No sites to display</h2>
<% end %>
How do I change the limit? .take hasn't worked and .limit hasnt worked
I'm guessing the problem is in your controller, not your view. You probably have something like
#sites = Site.find_by user_id: current_user.id
or
#sites = current_user.sites.first
Note in both cases #sites will be a single Site, not an array. This would produce an array though:
#sites = current_user.sites.limit(1)
Also btw in your elsif #sites.is_a? Site, saying #sites.take(8).each doesn't make sense because it's not an array.

Spliting and formatting an array of strings into list items on layout

I have a two-way many to many relationship between work and category models. When I pluck the categoies to show on my work#index
HTML:
<% #works.each do |work| %>
<article class="work-item" data-project="<%= work.id %>">
<header class="w-article-title ">
<%=raw work.svg %>
<h1 class="article-name"><%= work.name %></h1>
<ul class="categories-total n-visible">
<li><%= work.categories.pluck(:name) %></li>
</ul>
</header>
</a>
</article>
<% end %>
It ends up returning an array of strings like:
["Visual Design", "Strategy + UX", "UxD"]
How can I make the layout display:
indiviual list items?
Remove the brackets
trim the "" off the string
<ul class="categories-total n-visible">
<% work.categories.pluck(:name).each do |n| %>
<li><%= n %></li>
<% end %>
</ul>

Resources