Is there any elegant way to show active hyperlink style? - ruby-on-rails

Maybe I've missed something, but I've found that it's quite difficult to show conditional attributes in rails. Many programmers use helper methods to achieve this, but it still seems like a tedious task. I wonder if anyone has an elegant method of achieving this.
To be clear, let's say we have some css component as below (this example comes from a github opensource css framework: https://primer.style/css/components/navigation), So how would I change an 'aria-current': 'page' attribute?
<nav class="UnderlineNav UnderlineNav--right">
<div class="UnderlineNav-body">
<a class="UnderlineNav-item" href="#url" aria-current="page">Item 1</a>
<a class="UnderlineNav-item" href="#url">Item 2</a>
<a class="UnderlineNav-item" href="#url">Item 3</a>
<a class="UnderlineNav-item" href="#url">Item 4</a>
</div>
</nav>

Finally, I solve it by a helper method as the follow. If you guys have some other simple way, please let me know. Many thanks.
def active_header_link(text, slug, options)
if current_page? slug
link_to text, slug, {'aria-current': 'page'}.merge(options)
else
link_to text, slug, options
end
end
then, just call it when necessarily like this
<%= active_header_link node.name, node_path(node.slug), class: 'UnderlineNav-item' %>

Related

Display all Ghost tags

I would like to add all my tags to a footer section. {{tags}} seems to only work within a post, but not somewhere in the default template.
How can I display a full list of all tags created?
I managed to get my result using the Ghost API, however I would be surprised if this is the only way.
You should use the get helper:
{{#get "tags" limit="all"}}
<ul class="tags">
{{#foreach tags}}
<li>
{{name}}
</li>
{{/foreach}}
</ul>
{{/get}}
You can read more about get helper on docs.

Angular 2 Dart: Template syntax - how to concatenate strings?

Feels like a dumb question but I do not get it. How can I do fast string concatenation in Angular 2 Dart templates?
I have a seperate html file for my component lets say my_component.html:
Works:
....
<div id="abc">
{{order.pickupPlace.email}}
</div>
...
Works:
....
<div id="abc">
{{ ((order.pickupPlace.state) ? order.pickupPlace.state+" ":'')}}
</div>
...
Does not work:
....
<div id="abc">
{{"<br/>"+order.pickupPlace.email}}
</div>
...
Does not work:
....
<div id="abc">
{{order.pickupPlace.name+" "+order.pickupPlace.email}}
</div>
...
Have tried to find an answer in the docs here (https://webdev.dartlang.org/angular/guide/template-syntax#!#expression-operators) but no luck.
Of course I could use *ngIf on every element which I output conditionally but is there a way for simple string concatenation?
The best way is to declare a getter inside your Component controller that does the concatenation for you, you will get dart syntax support and the html template will looks cleaner.
String get myConcatenation => "${order.pickupPlace.name}${order.pickupPlace.email}";
<div id="abc">
{{myConcatenation}}
</div>
The last two examples can be made to work easily:
<div id="abc">
<br/>{{order.pickupPlace.email}}
</div>
And:
<div id="abc">
{{order.pickupPlace.name}} {{order.pickupPlace.email}}
</div>
Angular handles this pretty well. If you need some more complicated logic you can move it to the Dart code (but you cannot use HTML there easily).
If you find creating lot of weird logic consider creating more smaller components that can handle this for you.

adding a Font Awesome icon to phone_number_link helper and display inline

Hey all I am having a bit of an issue with adding a Font Awesome telephone icon to a helper method I used from a tutorial and not too sure how to proceed.
I am looking to achieve something like this:
but so far have only managed to create the telephone number as a clickable link.
the helper method:
def phone_number_link(text)
sets_of_numbers = text.scan(/[0-9]+/)
number = "+1-#{sets_of_numbers.join('-')}"
link_to text, "tel:#{number}"
end
and the link its self:
<li><%= phone_number_link("(587) 841-0708") %></li>
Im just not sure how to add the icon into this equation. Any assistance here would be greatly appreciated!
please let me know if you require any further information.
Try this :
<li><i class="icon-phone"></i><%= phone_number_link("(587) 841-0708") %></li>
Assuming you have included the font awesome css properly (and we should verify that), you should only have to do this:
<i class="fa fa-phone" aria-hidden="true"></i>
... so in the context of your code it would look like this:
<li>
<i class="fa fa-phone" aria-hidden="true"></i><%= phone_number_link("(587) 841-0708") %>
</li>
Here's the Font Awesome phone icon

<data:post.title> returns title of blog and not post title

Hi I'm trying to convert a static html and css design to a blogger template. I've run into an issue where i expect the post includable to return the post title but instead is returning the blog title. Here is the includable code for the post and below it is the include code that is called in the main includable that houses the html structure. Please any ideas as to how i might resolve this would be very grateful.
<b:includable id='post' var='post'>
<!--Article Summary-->
<b:if cond='data:post.title'>
<h1 class="article-title">
<data:post.title/>
</h1>
<b:else/>
<h1 class="article-title">
<data:post.title/>
</h1>
</b:if>
<p class="posted-by">
by
<em class="author-name"><data:post.author/></em>
on
<span class="date"><data:post.dateHeader/></span>
</p>
<p class="article-content">
<data:post.body/>
</p>
READ MORE
</b:includable>
<b:if cond="data:blog.url == data:blog.homepageUrl">
<div class="article-summary">
<img class="article-image" src="https://lh3.googleusercontent.com/article-main-image.jpg" alt="article-main-image"/>
<b:include name="post"/>
</div>
</b:if>
is correct, so instead I'd be curious about whether or not you're including from the right place. For example, are you in the MOBILE section vs the NON-MOBILE section of the template?

Rails: Rendering Multiple index.html.erb on a Single Page

I am using tabs and want to load multiple index pages into tabs. For instance:
class AnimalsController < ApplicationController
def index
#dogs = Dog.all
#cats = Cat.all
end
end
Then in my views/animals/index.html.erb
<ul class="tabs">
<li>Dogs</li>
<li>Cats</li>
</ul>
<div id="#dogs">
<%= render #dogs %>
</div>
<div id="#cats">
<%= render #cats %>
</div>
Is refactoring out into a partial the only way to achieve this?
I'd like to have them loaded statically at once and not have to resort to doing an Ajax.load() when the tab is clicked.
You have your answer in the question itself. Why don't you just use javascript to hide the two partials and call them when their respective tab is clicked? You don't need ajax for this at all :)
Since you did not mention the javascript library that you use, I will give a generic solution using jquery:
Also you need not add a # to your respective div's ids. Change it to this:
<ul class="tabs">
<li id="dogs">Dogs</li>
<li id="cats">Cats</li>
</ul>
<div id="dogs" class="subTabs">
<%= render #dogs %><hr />
</div>
<div id="cats" class="subTabs">
<%= render #cats %><hr />
</div>
$(document).ready(function() {
$('.subTabs').hide(); //Hide the subTabs as soon as the DOM is loaded.
$('li').live('click', function(e) {
$('.subTabs').hide(); //Calling this again so as to remove an already loaded
tab, if any. You can refactor this part to make it
even simpler.
$('body').find('.subTabs').attr('id',$(this).attr('id')).show();
//This finds the ".subTabs" whose id is the same as the "li" id that
was clicked and shows it. Ofcourse, even this can be made even more
compact had i known your entire DOM structure.
});
});
Edit:
You also have to make sure that you style it using CSS to make it look more like tabs if you haven't already. :)
Hope this helps. :)
You typically only want to use a partial if you are using the same or almost the same code in more than one place.
When you say "index page", do you really want to use the same code that is used in an index of another controller? If so, then a partial is the best strategy.
Don't use JavaScript to solve a layout / code organization problem.

Resources