Link Resolving

When working with field types such as Link or Rich Text, the prismic.io Ruby kit will need to generate links to documents within your website.

Before Reading

This page assumes that you have retrieved your content and stored it in a variable named document

Since routing is specific to your site, you will need to define your Link Resolver and provide it to some of the methods used on the fields.

If you are incorporating prismic.io into your existing Ruby project or using the Ruby on Rails starter project, you will need to create a Link Resolver.

Here is an example that shows how to add a Link Resolver function.

Copy
def link_resolver()
  @link_resolver ||= Prismic::LinkResolver.new(nil) {|link|
    
    # URL for the category type
    if link.type == "category"
      "/category/" + link.uid

    # URL for the product type
    elsif link.type == "product"
      "/product/" + link.id

    # Default case for all other types
    else
      "/"
    end
  }
end

A Link Resolver is provided in the Ruby on Rails starter project, but you may need to adapt it or write your own depending on how you've built your website app.

The Link Resolver can be found in the app/helpers/prismic_helper.rb file of the Ruby on Rails starter.

Accessible attributes

When creating your link resolver function, you will have access to certain attributes of the linked document.

link.id

string

The document id

link.uid

string

The user-friendly unique id

link.type

string

The custom type of the document

link.tags

array

Array of the document tags

link.lang

string

The language code of the document

link.isBroken

boolean

Boolean that states if the link is broken or not