I have a navigation panel. I want the class of the menu item to change to active if I click on it.
%li= { :class => current_page?(admin_roles_path) ? 'active' : nil }
= link_to "Roles", admin_roles_path
But I get the following error:
Haml::SyntaxError in Admin::Roles#index
Showing /sites/freshapp/app/views/layouts/_sidebar.html.haml where line #7 raised:
Illegal nesting: content can't be both given on the same line as %li and nested within it.
Try this (you don't need = after %li)
%li{ :class => current_page?(admin_roles_path) ? 'active' : nil }
= link_to "Roles", admin_roles_path
You don't need = after your %li and you have too mush spaces / tabs. (Haml is a nested language, number of tabs / spaces matters). Do this:
%li{ :class => current_page?(admin_roles_path) ? 'active' : nil }
= link_to "Roles", admin_roles_path
Try this
%li{ :class => "#{current_page?(admin_roles_path) ? 'active' : nil}"}
= link_to "Roles", admin_roles_path
Related
I have this HAML code adding an "active" class based on an if statement.
= link_to 'Contact Information',
edit_account_path(:contact_information),
class: ("active" if params[:section] == 'contact_information')
I want to also add a string of classes permanently, outside of the if statement.
Something like
= link_to 'Contact Information',
edit_account_path(:contact_information),
class: "Tab large", ("active" if params[:section] == 'contact_information')
I need to do this without creating a helper method because I'm not allowed to edit the code too much.
Use the ternary operator:
= link_to 'Contact Information',
edit_account_path(:contact_information),
class: "Tab large #{params[:section] == 'contact_information' ? 'active' : ''}"
The second example:
= link_to 'Contact Information',
edit_account_path(:contact_information),
class: current_page?(edit_user_registration_path) ? 'active' : ''
create a helper method say find_classes,
def find_classes(section)
default_class = ['Tab Large']
if section == 'contact_information'
default_class << 'active'
end
default_class.join(' ')
end
and from your view,
class: find_classes(params[:section])
Advantage:
you can have multiple if's and can have various classes appended.
I want to pass a parameter using link_to. (Also I am trying to use Bootstrap tab)
ilban.html.erb
<%= link_to '일반공지', '#home', { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab', :where => 1 } %>
cpu_controller.rb
#where = params[:where]
This code doesn't get where as an parameter. How can I fix it?
I have not tested it, but it should pass the params that you want.
link_to "Search", searches_path(:where => 1, :when => "Today"), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }
Controller:
#where = params[:where]
In Rails 5, try this syntax for link_to
link_to 'Show', view_path(:id => view.id), { 'data-toggle' => 'tab', 'aria-controls'=>'home', 'role'=>'tab' }
In the place of view path you can edit with your controller path and pass the valid id that you need to link.
Or, try this syntax also to pass params
<%= link_to "Add car", {:controller => "car", :action => "add_car", :car => car.id }%>
And add in your controller
#car = Car.find(params[:car])
How do I add RDFa Lite 1.1 property="url" to my Rails link_to?
<%= link_to "Lipsum", my_path, property: "url" %>, naturally, won't work.
Desired outcome:
Lipsum
Source: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html
<%= link_to "Lipsum", my_path, { property: "url" } %>
In console:
irb(main):011:0> ActionController::Base.helpers.link_to('test', advisors_path, { property: 'url' })
=> "<a property=\"url\" href=\"/advisors\">test</a>"
How do I include the following functionality in the rails checkbox:
If checkbox is clicked show inputBox otherwise do not show inputBox. Below is not working.
= check_box_tag 'update',:onclick=>$('#inputBox')[this.checked ? "show" : "hide"]();
Like so :
= check_box_tag 'update', :onclick => "$('#inputBox')[this.checked ? 'show' : 'hide']();"
= check_box_tag 'update',:onclick=>'$(this).checked() ? $("#inputBox").show(): $("#inputBox")hide();'
beside the fact that accessibility standards discourage the use
of a link pointing to the current page, how I am supposed to
refactor the following view code?
#navigation
%ul.tabbed
- if current_page?(new_profile_path)
%li{:class => "current_page_item"}
= link_to t("new_profile"), new_profile_path
- else
%li
= link_to t("new_profile"), new_profile_path
- if current_page?(profiles_path)
%li{:class => "current_page_item"}
= link_to t("profiles"), profiles_path
- else
%li
= link_to t("profiles"), profiles_path
...
Thank you.
# helpers
def current_page_class(page)
return :class => "current_page_item" if current_page?(page)
return {}
end
-# Haml
#navigation
%ul.tabbed
%li{current_page_class(new_profile_path)}
= link_to t("new_profile"), new_profile_path
%li{current_page_class(profiles_path)}
= link_to t("profiles"), profiles_path
...
#navigation
%ul.tabbed
%li{:class => current_page?(new_profile_path) ? "current_page_item" :nil }
= link_to t("new_profile"), new_profile_path
%li{:class => current_page?(profiles_path) ? "current_page_item" :nil }
= link_to t("profiles"), profiles_path
...
Looks like a good case for a partial to me.