I have a list(ul) which when click you will be redirected to another page, what I want to do is disable the clickable link when you are already on that page and also, have it highlighted.
<div class="selection">
<ul>
<li>Name</li>
<li>Comments</li>
</ul>
TYIA!
You can try with a helper so you can use this on other parts, because you will need to.
def active_link(text, path)
class_name = current_page?(path) ? 'active' : nil
link_to text, path, :class => class_name
end
this will print a link with the class active if the link it's the same as the current page
active_link 'home', root_path
Now you can combine this with css so you can disable the click on the link when this have the active class
a.active {
pointer-events: none;
cursor: default;
}
then with this all the links that you print with the helper will have the active class and with the css, this wouldn't have events on the click.
You can make use of current_page?
<ul>
<% if current_page?('/name') %>
<li><strong>Name</strong></li>
<% else %>
<li>Name</li>
<% end %>
<% if current_page?('/comments') %>
<li><strong>Comments</strong></li>
<% else %>
<li>Comments</li>
<% end %>
</ul>
Use jQuery
HTML part
<div class="selection">
<ul id="menu">
<li>Name</li>
<li>Comments</li>
</ul>
jQuery
$(document).ready(function () {
$("#menu a").each(function(){
//set all menu items to 'black
$(this).css("color","black");
var linkPath=$(this).attr("href");
var relativePath=window.location.pathname.replace('http://'+window.location.hostname,'');
//set the <a> with the same path as the current address to blue
if(linkPath==relativePath)
$(this).css("color","blue");
});
});
enter link description here
after looking at your question, I think that the simplest answer to your solution while only utilizing html may be to get rid of the <a>...</a> tag on the source page so that when you're there, the text will show but is not linkable. As for having your text highlighted while on that page, use <mark>...</mark> as a child element of <li>...</li>.
An example, assuming that "name" is the active page:
<div class="selection">
<ul>
<li><mark>Name</mark></li>
<li>Comments</li>
</ul>
</div>
Hope this helps a bit.
Related
In my controller:
#xxxs = XXX.page(params[:page] || 1).per(10)
In my view:
<div class="turn_page>
<%= paginate #xxxs, window: 2 %>
</div>
Why the content in the end of the font is repeated again, but inside the tag, only the link has no html content
I am new to MVC and have encountered a problem. I have an HTML page that scrolls down to a section when that section's name is clicked from the navigation bar but now I have to change this HTML line of code to MVC:
<li><a class="page-scroll" href="#services">Services</a></li>
I have tried to use Html.ActionLink but have not had any success yet!
What I have so far:
#Html.ActionLink("Services", "Index", "Home", new { area = "#services" }, new { #class = "page-scroll" })
Create your own helper or use raw html
<li>Link Text</li>
OR
<%= Html.ActionLink("Link Text", "Action", "Controller", null, null, "services", new { parameter = "example"}, null) %>
On your Index page:
<div id="services">
<p>Here is the content you want to make show when you click the link</p>
</div>
When you click on 'Link Text' it will direct you to the services section of your Index page.
Try
#Html.ActionLink("Services", "Index", "Home#services", new { #class = "page-scroll" })
I'm creating a helper in my rails app where I create a navigation link.
Now I want to add an caret to this link so I can get a nice arrow at the end.
My html should look like this:
<li class='dropdown'>
<a class='dropdown-toggle' data-toggle='dropdown' href='#'
Dropdown
<b class='caret'></b>
</a>
Now I got my helpers setup like this:
content_tag(:li, class: 'active dropdown') do
link_to( text, link, class: 'dropdown-toggle' ) do
content_tag(:b, class: 'caret')
end
end
But when I do this I got this error message:
undefined method `stringify_keys' for "/":String
I also want to add some item to my dropdown so I need to nested some more but I don't know how. Is there anybody who could help me and point me in the right direction?
Thanks!
You’re passing a block to link_to so you shouldn’t pass it link text, as shown in the docs. Try this:
content_tag(:li, class: 'active dropdown') do
link_to(link, class: 'dropdown-toggle' ) do
"#{text}#{content_tag(:b, "", class: 'caret')}".html_safe
end
end
I'm trying to generate a link using the link_to helper that will output the following HTML:
<i class="some_class"></i>Link Name
However the code I'm using to try to accomplish this:
link_to(tag("i", class: options[:icon]) + title, url)
...is outputting:
<i class="some_class">Link Name</i>
Why is it doing this, and how can I fix it? Thanks.
EDIT:
I believe I found the issue.
<i> tags are not self-closable tags in HTML5. Therefore the text after the i is treated as that element's content.
Have you tried using the block format of link_to?
<%= link_to url do %>
<%= tag("i", class: options[:icon]) %>
Link Name
<% end %>
Tweak that to your needs and maybe you'll get what you're looking for.
This is the icon tag helper I use in my applications which I frequently pass as the first argument to link_to, which can either be used to create a icon tag alone, or an icon tag followed by text.
def icon_tag(icon, *args)
options = args.extract_options!
text = args.first || options.delete(:text)
if text.nil?
content_tag :i, "", class: ["icon", "icon-#{icon}"] + options[:class].to_a
else
"#{icon_tag icon} #{text}".html_safe
end
end
In coding layout is the link styled like
<span>Previous</span>
<a href="#" class="class1 class2 class3"><span>Next</span>
I tried to search and I found some possible modifications of a shape of these two links. It's:
<div class="pag">
<%= will_paginate #data, :page_links => false, :next_label => '' %>
</div>
<div class="pag">
<%= will_paginate #data, :page_links => false, :previous_label => '' %>
</div>
My question: How can I update the second example to the shape of the first example? My problem is those two span elements...
Thanks
It's can be very easy - just add html markup for your paginate components to your config/locales/en.yml file
for example:
en:
hello: "Hello world"
will_paginate:
previous_label: <span class="glyph glyph-prev"></span>
next_label: <span class="glyph glyph-next"></span>
It's all!
http://thewebfellas.com/blog/2010/8/22/revisited-roll-your-own-pagination-links-with-will_paginate-and-rails-3 You can have your own formatter so you can customize anything you want.
Wiki
en:
will_paginate:
previous_label: "← Previous"
next_label: "Next →"
page_gap: "…"
https://github.com/mislav/will_paginate/wiki/I18n#translating-will_paginate-output