Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleman check if page exists

I have a language menu such as this (it switches the current page to another language):

- path = current_page.path.split('/')[1..-1].join('/')
%ul{ role: 'langmenu'}
  -if I18n.locale != :en
    %li
      =link_to 'English', "/en/#{path}"
  -if I18n.locale != :sr
    %li
      =link_to 'Serbian', "/sr/#{path}"

I want to show the link to the locale only if the destination page actually exists since not all pages will have translation. I tried with sitemap.find_resource_by_path("/sr/#{path}") but it always returns false even when it exists. What am I missing ?

like image 223
majkinetor Avatar asked Nov 16 '25 23:11

majkinetor


1 Answers

The argument of sitemap.find_resource_by_path is a resource path, not destination url path. So you must do something like this :

resource_path = current_resource.path.split('/')[1..-1].join('/')
sr_resource_path = sitemap.find_resource_by_path "sr/#{resource_path}"
if sr_resource_path
  return sr_resource_path.destination_path
end
like image 141
skoji Avatar answered Nov 19 '25 12:11

skoji