<a>

<a> is extended in Rapid to automatically provide URLs for Hobo model routes

Usage

The tag behaves as a regular HTML link or anchor if either the href or name attribute is given:

<a href="/admin">Admin</a> -> Output is exactly as provided, untouched by Rapid

If no href or name is given then the context is used to determine the link URL. The helper method object_url is used to construct the URL using restful routing:

If the context is a class then the link will be an index page:

<a with="&BlogPost">My Blog</a> -> <a href="/blog_posts">My Blog</a>

If the context is a hobo model instance then the link will be a show page:

<% blog_post = BlogPost.find(1) %>
<a with="&blog_post">My Blog Post</a> -> <a href="/blog_posts/1">My Blog Post</a>

An action can be provided for an alternative show page:

<a with="&blog_post" action="edit">Edit Post</a> -> <a href="/blog_posts/1/edit">Edit Post</a>

Or a new page if the context is a class:

<a with="&BlogPost" action="new">New Blog Post</a> -> <a href="/blog_posts/new">New Blog Post</a>

Additional Features

  • If the constructed route does not exist then the link will not be created, but the content of the link will still be output. E.g. when /blog_posts does not exist (because the hobo model controller does not exist or the index action is disabled):

      <a with="&BlogPost">My Blog</a> -> My Blog
    

    when the show action /blog_posts/:id does not exist:

      <a with="&blog_post">My Blog Post</a> -> My Blog Post
    
  • If no content text is provided then <a> will use the name method on the context to provide the text. E.g.

      <a with="&blog_post"/> -> <a href="/blog_posts/1">My First Blog Post</a>`
      <a with="&BlogPost"/> -> <a href="/blog_posts">Blog Posts</a>`
    
  • If action="new" then <a> will check that the current user has permission to create the object
  • Several useful classes are added automatically to the output <a>.

AJAX

The standard AJAX form attributes are supported. This can be used for conventional Ajax:

<a href="&request.fullpath" update="results">Refresh results</a>

Or it can be used with push-state to provide faster links:

<a href="somewhere/else" update="body" push-state new-title="Somewhere else">Go somewhere else.</a>

In this case the entire body is being refreshed but the headers aren’t, so will work to link between two dissimilar pages as long as the headers are identical.

AJAX attributes are supported in both modes: in the “magic” mode where RAPID determines the href target or in “passthrough” where you supply the href or name.

Note that <a> does NOT set the page_path parameter the way that <form> does. This means that Hobo will attempt to render a part in the DRYML for your destination path rather than a part in the current DRYML. This will only work if either the source and the destination are the same as in our first example, or if the source and destination have a similar part layout.

This is desired behaviour; <a> usually means ‘go somewhere else’ after all. If you want to set page_path, you’re usually better off using a real form:

<form action="somewhere/else" update="foo"><submit label="update foo"/></form>

Attributes

The standard AJAX attributes are supported.

  • action: If “new”, triggers the special behaviour listed above. Otherwise, contains the action to be performed on the context. If action is not specified specified, the action will be “index” or “show”, as appropriate.

  • to: Use this item as the target instead of the current context.

  • params: These are appended to the target as a query string after a “?”. Params are passed as a ruby hash. Example: params="&{:id=>17, :name=>'joe'}"

  • query-params: if set, current query parameters are mirrored onto the link. If true or blank, all non-ajax query parameters are used. (query-params, query-params="&true" or query-params="") Otherwise, the specified query parameters are used. (query-params="page,sort,search")

  • href, name: If either of these attributes are present, the smart features of this tag are turned off.

  • format: this adds “.#{format}” to the end of the url

  • subsite: routes the URL using the subsite

  • force: overrides the permission check if action is “new”

  • method: “get”, “put”, “post” or “delete”. “get” is the default. This parameter assumes jquey-rails is being used, and simply sets data-method as well as rel=”nofollow”.

  • only-path, protocol, host, subdomain, domain, tld-length, port, anchor, trailing-slash: In smart mode, passed through to Rails’ polymorphic_url. For more information, see the Rails documentation for url_for.

Show Source


Edit this page