prismicio-community/ruby-kit

undefined method `link_to' for #<Prismic::Ref:0x00000005995f20>

Closed this issue · 2 comments

Hi. I have an article which has references.

However, having the article and calling the method as_html(api.master_ref) I get the following error: undefined methodlink_to' for #Prismic::Ref:0x00000005995f20`. I suspect that I have to add a custom link_resolver so that all documents have my website's base url. However, I haven't found anything relevant in your API or ruby-kit documentation. The only thing I managed to do is to add a custom link_to method in Prismic::Ref object that maps to my website like:

ref.class.send(:define_method, :link_to, ->(link) { "#{link.type.pluralize}/#{link.slug}" })

I guess the proc body could be a class or my custom link_resolver method. But I am wondering, is this the way to go?

Here is a simple link resolver we use in a rails project:

  def link_resolver(ref)
    @link_resolver ||= Prismic::LinkResolver.new(ref) do |doc|
      case doc.link_type
      when "article"
        titled_blog_path(doc.id, doc.slug, ref: ref)
      else
        raise "link_resolver doesn't know how to write URLs for #{doc.link_type} type."
      end
    end
  end

It's stored in a Helper and we call it in a view like so:

@post.get_structured_text('article.content').as_html(link_resolver(@ref)).html_safe

I know this might not be the clearest answer but I'll clarify where needed.

ok.. thanks!