How can I display a link to a page managed by comfy cms only if the page exists in a ruby on rails application?

25 Views Asked by At

I am using comfortable_mexican_sofa 2.0.0 in a rails application. I have a table with many rows which displays instances of a model. In my model definition, I have a method which makes a clickable link to a page with some specific data/content related to that instance/record.

class MyModel < ApplicationRecord
  def table_button
    "<a target='_blank' href='http://localhost:3000/#{self.id}'>link to the page</a>"
  end
end

This link should only be displayed if the corresponding page exists. How can I do that ?

1

There are 1 best solutions below

0
Maxime On

First, method to check if the page exists

  def page_exists?
    Dir.glob("#{ Rails.root }/path/to/the/page") != []
  end

Second, display the link if the page exists

def page_link
  page_exists? ? "<a target='_blank' href='some_url_to_that_page'>Go to the page</a>" : ""
end