Separate link_to with comma - ruby-on-rails

I want to separate this block with commas :
- game_publication.groups.each_with_index do |group, index|
= link_to store_group_path(current_store, group) do
%span= #groups.find(group).name.to_s + (index > 0 ? ', ' : '')
But for the moment it returns something like
<label>Groups :</label>
<a href="/66-store/groups/4594?locale=en">
<span>party hard</span>
</a>
<a href="/66-store/groups/5063?locale=en">
<span>b0m,</span>
</a>
<a href="/66-store/groups/5066?locale=en">
<span>test,</span>
</a>
</label>
It doesn't seems a situation where I can use any rails helpers.
I would like something like group1, group2, group3.
<label>Groups :</label>
<a href="/66-store/groups/4594?locale=en">
<span>party hard,</span>
</a>
<a href="/66-store/groups/5063?locale=en">
<span>b0m,</span>
</a>
<a href="/66-store/groups/5066?locale=en">
<span>test</span>
</a>
</label>

First, are you sure that you pasted here the exact code which gave the posted result? In your code, you have
(index > 0 ? '' : ',')
which means: Do not add a comma UNLESS we are on the first element. The result you posted had the comma the otherway round: It has a comma everywhere, EXCEPT for the first element. With other words: The code you posted, can't produce the output you posted.
Now for your problem: You want to add a comma on every element, except the last. This means that you need to know the highest (last) index value:
last_index = game_publication.groups.size - 1
With this, you can write your expression as
(index == last_index ? '' : ',')

Related

In a Thymeleaf `th:text` tag, how do I show different text on `if int == -1`?

I have this in a Thymeleaf web page ...
<span th:text="${account.balance}">balance</span>
When the account.balance == -1 that means unknown, so I want to display text "Unknown".
How do I do an if account.balance == -1 show 'Unknown' in Thymleaf?
There are a whole lot of ways to do this... I would just suggest reading through the Thymeleaf conditional evaluation documentation.
I'd probably do something like this, myself:
<th:block th:switch="${account.balance}">
<span th:case="-1">Unknown</span>
<span th:case="*" th:text="${account.balance}" />
</th:block>
You could also do some thing like this:
<span th:if="${account.balance == -1}">Unknown</span>
<span th:unless="${account.balance == -1}" th:text="${account.balance}" />
Or you could build a String:
<span th:text="${account.balance == -1 ? 'Unknown' : account.balance}" />
This should work: <span th:if="${account.balance == -1}">Unknown</span>
But this : page will be very much helpful for conditionals in general

Thymeleaf : 2 loops and an if statement inside a single element

In thymeleaf, I have a for each loop like so:
<ul class="days">
<li th:each="day : ${days}" th:text="${day}" >1</li>
</ul>
This successfully lists all of the strings in an array called days which is populated like ["1","2","3"..."31"] representing the days in a given month.
I also have an array of items which also contains days as strings.
Here is what I want to do in pseudo code but am struggling to figure out how to achieve it:
For each day in days; For each day in items ; if items.day =
days.day then set 'class=active' (bootstrap) and th:text =days.day
else
th:text=days.day
So if theres a day in items that matches a day in days then the <li> element is set to class=active and make the <li> clickable with href="/myurl". And either way the day from days is the th:text of the <li>.
Sorry if thats hard to understand, I tried to make it as clear as I could.
EDIT:
This is latest attempt:
<ul class="days">
<li th:each="s : ${days}" th:with="found=${false}">
<span th:each="item : ${items}" th:if="{$item.day == s}">
<span th:text="${s}" th:classappend="active" th:href
th:with="${found} = true"></span>
</span>
<span th:if="{$found == false}">
<span th:text="${s}"></span>
</span>
</li>
</ul>
I think you can achieve this with conditional expressions. Simply write something like
<span th:text="${s}" th:classappend="${#lists.contains(items, s)}? 'active' : ''">
This code also uses #lists, a very useful feature of Thymeleaf.

Conditionally add hide-xs attribute

In an ng-repeat I want to add a hide-xs attribute on an element based on the current scope.
How can I do that?
I basically want to do something like (this obviously don't work):
<li ng-repeat="item in items" hide-xs="{{ item.showAlways ? 'false' : 'true' }}">
{{item.title}}
</li>
Edit
I ended up doing this (as suggested by DieuNQ) but if anybody know how to do it using directive and not class I would take it
<li ng-repeat="item in items" ng-class="{'hide-xs': !item.showAlways }}">
{{item.title}}
</li>
hide-xs not works like that (it does not depend on true or false). It just add class to your tag. Try this:
In your html:
<li ng-repeat="item in items" ng-model="item.showAlways" ng-class="someClass">
...
</li>
In your controller:
$scope.item.showAlways ? $scope.someClass == '' : $scope.someClass == 'hide-xs';

Thymeleaf-Not able to append '/' in html

<span th:each="entry: ${productOptionDisplayValues}">
<span th:if="${entry?.key != 'OfferStatus'}">
<span th:if="${entry?.key=='color_'}" th:text="${entry.value}+ ' / '"/>
<span th:unless="${entry?.key=='color_'}" th:text="${entry.value}"/>
</span>
</span>
I have the following span tag in my html where i am iterating on a map and printing the key value.
At the end of first if block , after printing the value i want to appand a '/' for which i am appending + ' / ' at the end of first if block.
But it's appearing as '아이보리 T38' where instead of / , T is getting appended after the color variance.
It should come as '아이보리 / 38'
If you want Thymeleaf to respect your XHTML or HTML5 tags and not escape them,
You will have to use a different attribute: th:utext (for "unescaped text").

Select following-sibling if element unknown

I want to select the first sibling of a tag from an HTML element. Lets say I want to get the <a> that follows this <span>:
<span id="unique"></span>
<a href="#">
This can easily be done with the following Xpath:
//div[#id="unique"]/following-sibling::a
This, however, is very specific in a way that it looks for an <a> tag. How can I make it more generic that it would find any tag as long as it is the first sibling to the element I selected?
Write as below
//span[#id="unique"]/following-sibling::*[1]
Here is a sample HTML :
<div>
<span id="unique"></span>
<p>foo</p>
<p>bar</p>
</div>
XPATH expression:
//span[#id="unique"]/following-sibling::*[1]
output
<p>foo</p>

Resources