Middman checks if the page exists

I have a language menu like 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 only want to show the link to the locale if the destination page actually exists, since not all pages will have a translation. I've tried with sitemap.find_resource_by_path("/sr/#{path}")

but always returns false even when it exists. What am I missing?

+3


source to share


1 answer


The argument sitemap.find_resource_by_path

is a resource , not a destination url path. Therefore, you should 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

      

+1


source







All Articles