piotrmurach / loaf

Manages and displays breadcrumb trails in Rails app - lean & mean.
MIT License
407 stars 24 forks source link

current_page? returns false if on root_path #8

Closed philipgiuliani closed 7 years ago

philipgiuliani commented 9 years ago

If i am on the root_path, the crumb does not gets the styles.

A solution could be to check by controller and action instead of the url.

Example:

# routes.rb
root "publications#index"
resources :publications, only: :index
# publications_controller.rb
breadcrumb "Publications", publications_path

If i am on the /publications the crumb is highlighted. If im on / instead, its not.

piotrmurach commented 9 years ago

As breadcrumb second argument you can use anything that Rails accepts including parameterized hash {controller: :publications, action: :index} ?

philipgiuliani commented 9 years ago

Wow thanks. It really worked.

philipgiuliani commented 9 years ago

Idea how to solve it if its namespaced?

piotrmurach commented 9 years ago

I would try again Rails conventions, so as controller parameter do controller: '/admin/publications' or similar. The leading slash will ensure that the url gets resolved as Admin::Publications. Does it work?

philipgiuliani commented 9 years ago

Ok i found the problem. You are currently calling url_for(controller: ...., action: ....) before passing it to current_page?.

If i have:

# routes.rb
root "publications#index"
resources :publications, only: :index

and call url_for the following is returned:

url_for controller: "/publisher/publications", action: :index
# => http://localhost:3000/de/publisher

The Problem When i go to http://localhost:3000/de/publisher everything is fine, and it is highlighted. But when i go to http://localhost:3000/de/publisher/publications, current_page? returns false with this URL.

Would be cool if you could pass the hash directly to current_page?. I think that should be the solution :) Thanks and sorry for distubing!

Update

The issue also causes the folowing problem. The only thing i left away is the string at the end thats created by to_param

url_for(...)
# => http://localhost:3000/de/publisher/publications/1-dolomiten/magazines/73-ausgabe-3-2014
current_page?("http://localhost:3000/de/publisher/publications/1-dolomiten/magazines/73")
# => false
piotrmurach commented 9 years ago

Ok, understood. Do you want to take a stab at it and submit pull request? I will happily review and merge - provided there are tests demonstrating the issue :smile:

piotrmurach commented 7 years ago

It's been a long time since this issue has been posted - sorry!

However, I've finally fixed the problem in master branch and made the solution super flexible. I've added :match to allow for granular setting of matching behaviour when highlighting current breadcrumb. Hope you like it!