After someone pointed link_to_unless_current out to me, I would like to apply it to my dashboard sidebar. However, I can't get it to work, so I'm guessing I'm doing something wrong.
This is my current sidebar
.dashboard_bar
%ul
= link_to admin_dashboard_path do
%li.icon-dashboard
= link_to admin_cs_dashboard_path do
%li.icon-heart
= link_to admin_dashboard_path do
%li.icon-money
= link_to admin_dashboard_path do
%li.icon-group
= link_to admin_dashboard_path do
%li.icon-bug
= link_to admin_dashboard_path do
%li.icon-hdd
When I change link_to to link_to_unless_current, it just screws up my css and the icons are no longer clickable.
Can someone enlighten me on how to fix it?
P.S. I did check out the Rails guide on this, but still can't seem to figure it out.
The most probable reason is that your are on the same url as admin_dashboard_path. So, all your links are disabled and shown as text.
Ok, after sitting down with someone who knows more about Rails, we came to the following solution:
.dashboard_bar
%ul
%li
= link_to_unless_current '', admin_dashboard_path, :class => "icon-dashboard" do
%span.icon-dashboard
%li
= link_to_unless_current '', admin_cs_dashboard_path, :class => "icon-heart" do
%span.icon-heart
%li
= link_to_unless_current '', "#", :class => "icon-money" do
%span.icon-money
%li
= link_to_unless_current '', "#", :class => "icon-group" do
%span.icon-group
%li
= link_to_unless_current '', "#", :class => "icon-bug" do
%span.icon-bug
%li
= link_to_unless_current '', "#", :class => "icon-hdd" do
%span.icon-hdd
The main problem was that the links were around the li, while they should be inside the li. With applying the class to the link with :class => "icon-*" and to the span, we managed to create the look and functionality I was looking for.
As soon as link_to_unless_current sees that I'm on the current page of that link, it changes the link to a span, creating possibilities to use css on the icons for both the current page and the other links.
Passing a block to #link_to_unless_current does not behaves as passing a block to #link_to : the content of block is used as an alternative content if link is indeed current path.
So, when you write :
= link_to admin_dashboard_path do
%li.icon-money
What you actually say is : make a link to admin_dashboard_path with nil name. But if it's the current path, write <li class="icon-money"> instead.
If you want to achieve what you wanted (the list item inside link), you should go something like that rather :
= link_to_unless_current '<li class="icon-money"></li>'.html_safe, admin_dashboard_path
This being quite odd in a haml file, the best solution is probably to keep your links inside your list items.
Related
Here is the offending line:
<p><%= link_to 'Job Type template', :action => :jt_template, :style => "color:white", :class => "btn btn-primary" %></p>
And when we load the page and inspect the element:
Job Type template
Why are my class and style tags being interpreted as params for the action?
Other than this weirdness - it's working as expected.
Just FWIW - the jt_template action sends the CSV file as a download. I was linking directly to the file stored in my public dir, but I found that when you clicked the button in Safari it would just open the CSV in the browser, and I'm forcefully required to make the file download instead (even though a user could get from the opened page)
EDIT:
Ever do that thing where you post to SO, then think of some new wording you haven't Googled, then find your answer on the first result?
Add css class to rails link_to helper
Someone go ahead and post the answer "you should put your :action inside { } and it will work" and I'll accept that.
Do this:
<p><%= link_to 'Job Type template', {:action => :jt_template}, :style => "color:white", :class => "btn btn-primary" %></p>
:action and :controller must be placed between { } in order to avoid the incorrect interpolation of stuff that comes after it (I only tried with :class and :style, but I assume it would affect anything that follows)
I need to have path like "#privacy" for tab plugin. Link must contain only anchor. When I use link_to 'Privacy', :anchor => 'privacy' Rails generate /privacy#privacy - link, that contains full path and anchor.
How can I told Rails to generate url without path (only anchor)?
Thanks.
Solved: link_to 'Privacy', '#privacy'
The following will create a link the way you want -
link_to "my-privacy", "#privacy"
In most browsers, the path of the current page will be prefixed, but if you check the source of the page, the following html will be seen -
my-privacy
This will most probably serve your purpose for the UI, just that you'll have to split the url at '#' using Javascript.
This will work for you
<%= link_to "title", resource_path(:anchor => "anchor") %>
No generation of routes is needed also no generator is needed.
<%= link_to "link text", "#", :id => 'your_id_here' %>
You will need the id to access the object via jQuery.
//edit
<%= link_to "link text", "/#anchor", :id => 'your_id_here' %>
Your best bet for this exact scenario is to just use <%= link_to "link text", "#anchor" %>. The anchor tag is used within url_for and doesn't really give you a clean way to just use an anchor.
I have a simple navigation menu with plain html links that target different pages. All of these pages share this menu, which is separated out in a partial.
I would like to have the current page's link be highlighted in bold. A generic version of my (non-working) implementation looks like this:
# 'pagetwo' view
<%= render :partial => "shared/nav" %>
# partial
<%= link_to "Home", home_url, :id => "home" %>
<%= link_to "Page 1", pageone_url, :id => "pageone" %>
<%= link_to "Page 2", pagetwo_url, :id => "pagetwo" %>
Do I solve this with JavaScript?
The link_to_unless_current suggestion appears to be working. Does anyone know how to assign different CSS class to each outcome using this method?
Unless you need to change the boldness of the links between page requests (i.e. in response to client side events), you might look into using the block form of link_to_unless_current or, for a more general solution, look into current_page?
Another option might be adding classes to the body (or similar container element) based on your current page and then writing CSS rules to target links in specific contexts. For example:
body.home a#home_link { font-weight: bold }
body.about a#about_link { font-weight: bold }
Here is the solution I found, which is the most compact I have yet seen. It builds on the answer given by #thatothermitch (thank you and +1!).
I used link_to_unless_current for all the links, i.e.
# partial
<div id="manage_list"> # already surrounds the entire partial
<%= link_to_unless_current "Home", home_url, :id => "home" %>
<%= link_to_unless_current "Page 1", pageone_url, :id => "pageone" %>
<%= link_to_unless_current "Page 2", pagetwo_url, :id => "pagetwo" %>
</div>
I then added CSS styles that pick up on whether the <a> tag exists:
#manage_list p{ font-weight:bold; }
#manage_list p a{ font-weight:normal; }
This way, there is no need to pass any variables, write any javascript, or add any helper.
I hope this is helpful for any others who are experiencing the same problem. :)
You can do this a simple helper
In application helper put this
def set_selected(url_hash)
current_page?(url_hash) ? "current" : ""
end
Your links will call the helper to set the class name
<%= link_to "Home", home_url, :id => "home", :class => set_selected({:controller => 'homes', :action => 'index'}) %>
<%= link_to "Page 1", pageone_url, :id => "pageone", :class => set_selected({:controller => 'page_ones', :action => 'index'}) %>
CSS definition for the current class goes in the application.css or any other css file in your application
.current{
font-weight:bold;
}
For the life of me, I can't figure out (or find the right text to search) how to create a link that looks like this:
<span>This text will be hidden</span>
There's a similar example in the link_to API, but it doesn't quite get to what I need. I don't want my anchor tag to have any text (all text will reside in the nested span) and I want to link to a route named publisher_root.
I'm tired of banging my head on this, so any help would be much appreciated.
UPDATE: As mentioned in my comment below, HAML is also in play here. I eliminated it originally because it seemed like nothing more than an additional complexity. Since it appears to be at the crux of the issue, though, I've added the tag and here's my code:
#masthead.container
%h1
!= image_tag( 'home-masthead.png' )
%p
- link_to publisher_root, :class => 'button first' do
%span Link Text
You need to use the block form of the link_to helper. This will do what you require:
<% link_to publisher_root, :class => 'button first' do %>
<span>This text will be hidden</span>
<% end %>
HAML version:
= link_to publisher_root, :class => 'button first' do
%span This text will be hidden
I'm trying to create unique anchors for every comment on my blog so a person can take the url of an anchor and paste it in their browser, which will automatically load the page and scroll down to the point in the page where their comment starts.
Perhaps I'm going about this the wrong way but I've tried this which was to no avail.
Comment view - Fail 1 - when pasted in a browser this link does not scroll down to the desired position
<%= link_to '#', :controller => 'posts', :action => 'show', :id => comment.post, :anchor => 'comment_' << comment.id.to_s %>
Comments controller - Fail 2 - Correct url in browser but no scrolling happens it just stays at the top of the page
redirect_to :controller => 'posts', :action => 'show', :id => #post, :anchor => 'comment_' + #comment.id.to_s
If someone could help I'd be very grateful :)
UPDATE: The solutions below almost work, however I come out with the following URL which isn't being scrolled to if I click on it.
#
i.e. http://localhost:3000/posts/please-work
Actually, anchor is an option for the path, not for the link_to
<%= link_to '#', post_path(comment.post, :anchor => "comment_#{comment.id}") %>
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M001565
link_to "Comment wall", profile_path(#profile, :anchor => "wall")
# => Comment wall
It looks like you want to use the link_to code that you have in your question. Then in your list of comments you have to make sure that you have an anchor tag named the same thing in the link.
So this:
<%= link_to 'Your comment', post_path(#comment.post) + "#comment_#{#comment.id.to_s}" %>
will generate something like this
Your comment
/* html code */
<a name="comment_1234">This is a comment</a>
You have to manually tack on the #comment_ otherwise the link_to method thinks that the :anchor attribute that you are passing it is for that tag.
Here's an improvement on #XGamerX's answer.
<%= link_to '#', [comment.post, { anchor: dom_id(comment) }] %>
Or
<%= link_to '#', post_path(comment.post, anchor: dom_id(comment)) %>
Try this:
<%= link_to '#', post_path(comment.post), :anchor => "comment_#{comment.id}" %>
this is best way:
<%= link_to '#', post_path(comment.post, anchor: dom_id(comment.id)) %>
These links will scroll down to position where you have code like:
<a name="comment_1"></a>
I don't know if there are helpers that will do it for you, but it is very simple and you can write your own.