I'm trying to making the final list element a clickable link to my search_path in rails using HAML but have been unable to make it work.
Here is what I have so far:
%li
= link_to('sign out', destroy_user_session_path, :method => :delete)
%li
= link_to "profile", edit_user_registration_path
%li{:style => 'background-color:#3D8599;'}
%i.fa.fa-search
Everything I have tried results in the fa-search icon being put in the wrong place or not being clickable.
Have you tried this?
%li{:style => 'background-color:#3D8599;'}
= link_to search_path do
%i.fa.fa-search
Related
I am configuring an upload form for a crowdfunding site.
The following code works. It gives me white text on the Edit Profile button:
<button class="btn btn-success large"><%= link_to 'Edit profile', edit_user_registration_path, {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%></button>
But when I try and add colors to the following line, Devise returns a syntax error:
<button class="btn btn-success large"><%= link_to "Logout", destroy_user_session_path, method: :delete {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%></button>
The problem is incorrect syntax/symbols between the words :delete and {:style. I have followed the error messages and tried every combination of symbols , : => ( and { but none are correct.
I'm making a syntax error, but am not sure what. Thanks if you can help.
Try this
<%= link_to "Logout", destroy_user_session_path, method: :delete,:style=>"color:#FFFFFF;", :class => "navbar-link css-class" %>
You have syntax error:
Replace
<%= link_to "Logout", destroy_user_session_path, method: :delete {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%>
with
<%= link_to "Logout", destroy_user_session_path, method: :delete, {:style=>'color:#FFFFFF;', :class => 'navbar-link' "css-class"}%>
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.
I'm brand new to ruby and rails and I'm having trouble creating a sign-out link (using devise for auth). I want to pass a custom :method parameter into the link_to function, and set a custom class. I seem to be able to do one or the other but not both.
When I try:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete, { :class => 'signout'} %>
I get the proper result from clicking the link, but I lose my styling. On the other hand, when I try:
<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout'}, :method => :delete %>
I get the styling I want but the link request is passed as GET rather than DELETE, resulting in a routing error.
What am I missing?
Try placing both :class and :method inside the hash.
So:
<%= link_to "Sign out", destroy_user_session_path, { :class => 'signout', :method => :delete} %>
You don't need hash here, because it is already hash :D
link_to "Sign out", destroy_user_session_path, method: "delete", class: "signout"
profit!
I am using the devise gem to make authentication work in my app.
Here's the code I have for signing out:
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
I have tried this as well:
<%= link_to "Sign out", destroy_user_session_path%>
Both of which when I click on sign out I get :
No route matches [GET] "/users/destroy"
However when I run rake routes, you can see it (just not GET):
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
How to fix this?
Mitch's answer was close but did not work for me, instead the following syntax did:
<%= link_to "Log out", destroy_user_session_path, :method => :delete %>
Could this help you in the right direction?
https://github.com/plataformatec/devise/issues/1195
Roger's link above is v useful.
I used the following syntax, which worked:
<%= link_to "Log out", destroy_session_path(:user), :method => :delete %>
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.