Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution to insert cross-hyperlinks of Rails objects inside text attribute (as HTML)

I am working at a web app in Rails that behaves much like a CMS: there are articles, which have a text attribute that contains links to other articles (or other object classes). I'm currently storing the attribute as HTML.

Is there any good way to model those links in a way that is relative easy to change, and contain the reference to the object id, instead of the absolute url?

One of the solutons I was thinking was to use some kind of special mark-up, such as:

[link_to "Text for the link", Article:12]

where 12 is the id of the article it links to. This mark-up will be parsed when the text is rendered.

The downside of this is that I have to hack into TinyMCE (the editor I'm thinking of using to edit the HTML) so that it can insert links to other objects, by accessing the database and automatically assigning the object type and ID (the person who's editing the texts doesn't know the id's).

Is there any simple solution to this?

Or should I stick to using absolute urls (which, besides maintenance issues, is annoying in development, as they will always point to production and that is confusing for me)?

Additionally, does anyone have similar examples in other languages (php, Wordpress, other CMS, etc) that tackle this problem in a nice way? I'm thinking, this is pretty vital in CMS, and can reduce a lot of man hours if a nice system can handle all those links.

EDIT:

Another possible solution that I'm thinking about is letting the person copy the link of the article directly in the code, but it should, upon submission, generate the correct association id and make it so that if the url structure changes, the link is always up-to-date. I'd like to hear your opinions and experience with this approach, if you have tried it.

The challenge with this approach is parsing the link with Rails and finding out that it points to an Article, and that article has the id ##. Then I have to insert a shortcode that will always translate, upon parsing and rendering, to an actual link to that article.

I found a method that could make this feasible:

Rails.application.routes.recognize_path

But there may be some caveats that I don't see right now...

EDIT no. 2

I also want to specify that I chose CKEditor as the content editor, but I will consider other ones if there are clearer advantages.

like image 797
Cristian Avatar asked Dec 09 '12 18:12

Cristian


People also ask

What is hyperlink in HTML what tag and attribute will you use for hyperlink?

The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address. Content within each <a> should indicate the link's destination.

How do you hyperlink text in HTML?

To make a hyperlink in an HTML page, use the <a> and </a> tags, which are the tags used to define the links. The <a> tag indicates where the hyperlink starts and the </a> tag indicates where it ends. Whatever text gets added inside these tags, will work as a hyperlink. Add the URL for the link in the <a href=” ”>.

What is hyperlink in HTML with example?

HTML links are hyperlinks. You can click on a link and jump to another document. When you move the mouse over a link, the mouse arrow will turn into a little hand. Note: A link does not have to be text. A link can be an image or any other HTML element!

How do you reference a link in HTML?

The <a> tag defines a hyperlink, which is used to link from one page to another. The most important attribute of the <a> element is the href attribute, which indicates the link's destination. By default, links will appear as follows in all browsers: An unvisited link is underlined and blue.


1 Answers

I have built something similar using a shortcode system which would allow me to call specific methods on the model and replace the shortcode in the text:

Helper

def parse_shortcode(model)
  text = model.text      
  text.gsub(/(\[#!\s?\w+\])/i) do |match|
    result = model.try(match)
    result.nil? '' : link_to(result[:text], result[:url])
  end
end

Model

def contact_link
  { :text => self.name, :url => self.url }
end

View

<%= parse_shortcode(@article) %>

I haven't tested the above code and it is obviously a bit simplified but it explains my thought process behind this.

EDIT: Just to clarify my above example uses an invented shortcode syntax of [#! method]

like image 160
Darren Coxall Avatar answered Sep 25 '22 06:09

Darren Coxall