Ruby - rails - how to create automatic hyperlinks for urls in the text/string rendered on the view? - ruby

How to create automatic hyperlinks for urls in the text/string rendered on the view?
I have a page that renders user activity log and in that log/text/string there are some random urls which I want to automatically hyperlink to open in a new browser window. There is this auto_link in ruby rails, how do I use that?
text = "User xyz accessed url - http://www.something.com on 04/13/2012 00:13:18GMT"
<%= "#{Text}" %>
I want this rendered with a hyperlink to the url. The URL could be anything anywhere in the text.

Use auto_link like this:
<%= auto_link(text) %>
If you want the generated links to open new browser windows (or tabs) add the option like so:
<%= auto_link(text, :html => { :target => '_blank' }) %>
As mentioned by pjumble in the comments, auto_link is no longer a part of Rails core as of Rails 3.1, this gem brings it back: https://github.com/tenderlove/rails_autolink Thanks pjumble!

For faster parsing, try https://github.com/vmg/rinku
require 'rinku'
Rinku.auto_link(text, mode=:all, link_attr=nil, skip_tags=nil)
Also one often need more filters than just linking, for example Youtube embedded videos. For this use: https://github.com/dejan/auto_html

Related

Using content_for and yield_content in Ramaze

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>
I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

HAML -> Backbone Template, Unescaping HTML Parameters

I'm using HAML to generate templates for a Backbone.js app. I need to be able to insert <%= blah %> as an html attribute a la:
%a{:href => "myresources/<% id %>"} My Resource
and have it output
<a href='myresources/<%= id %>' >My Resource</a>
in the html template. Unfortunately, HAML escapes the html parameters leaving me with
<a href='#myresources/<%= id %>'>My Resource</a>
According to the HAML Reference the '!' operator can be used for unescaping strings, but not within the HTML attributes.
Also, I'd use plaintext to render the anchor tag, but since the anchor tag is the root for this particular view, I lose all of the benefits of using HAML.
Any help?
Update
I didn't mention, but I'm using LiveReload to actually watch my file system and run the haml compiler, and there was a setting in LiveReload to disable HTML escapes in tag attributes. < head slap > If anyone else runs into this issue outside of LiveReload, you can also set the :escape_attrs option to false when configuring your HAML setup.
You can configure HAML to not escape tag attributes using the escape_attrs option in your HAML configuration. See HAML Options.
You can try using html_safe which is a method on String objects. This will escape the html characters in the variable statement (< for example) and will leave the intact for underscore to evaluate at runtime:
%a{:href => "myresources/<% id %>".html_safe} My Resource
Found on answer to Interpolate inside html attributes with Underscore.js

Not able to render partial using facebox with rails

I am using facebox to get some information in a popup window.
I have got facebox.js and facebox.css file in my assets folder. I have included this code in my application.js file
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox()
});
I have link_to which looks like this <%=link_to "Create",new_activities_path, :rel => "facebox" %>
When I click on the create link I get a popup which is empty and does not render the form which is in the partial.
I am able to render this partial without facebox. I have been looking all over the net but it seems the documentation of facebox is not that good.
Can anyone guide me?
Thanks,
Was able to do it by tweaking the code little bit :-
now my link_to looks like this <%=link_to "Create",new_activity_path, :remote => true %>
I also have a new action in the controller and a corresponding new.js.erb file which has got the below mentioned code:-
$.facebox('<%= escape_javascript(render :partial => 'new') %>')
the code in the application.js remains the same. I am able to render the partial in _new.html.erb in a nice popup.
Hope it will be useful to others as the documentation of facebox is very poor.
Thanks,

ruby on rails fleximage plugin

How do I display the image in a view?
The documentation says:
<%= image_tag formatted_photo_path(#photo, :jpg) %>
This is not what I want.
I just want to display an image on a view I don't care about the url.
E.g., Avatar.
Do I need to write a path to the directory or is a method already made?
/public/images/avatar/id
Thank you
Does the example above not render something like this to the view?
<img src="/path-to-generated-image.jpg"/>

Resources