I am looking for a Ruby script that takes a piece of text, and makes any links click-able that show up in it... Does anyone have something like this? For example, if I wrote a comment and said "Come check out my site at http://www.example.com", then the link would be click-able like an html tag.
Thanks,
Josh
Rails has a View helper called auto_link, e.g.:
<%= auto_link("Please visit my site: http://www.example.org/") %>
would produce:
Please visit my site: http://www.example.org/
Update: You can find more information about it here.
Related
I'm trying to implement the new Google recaptcha in my Rails 4 app but I'm unable to get the widget to display in Haml. Basically what I'm trying to do is to make this code
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
into somehting like this:
%div{class: 'g-recaptcha' data-sitekey: 'your site key'}
The way how I try to write it above gives me an error on the data-sitekey part.
I also tried to write it like this:
%div{class: 'g-recaptcha' 'data-sitekey' => 'your site key'}
and still no luck. Has anyone attempted to do this? Apparently there is not too many stackoverflow questions or online resources dealing with this. But if someone can help me with this, I would greatly appreciate it?
*Also, what is the data-sitekey considered? It's not a class or an id. What is it? Maybe I have the formatting wrong?
This is the most concise equivalent haml:
.g-recaptcha{data: {sitekey: 'your site key'}}
Your second example would also be equivalent, but you're missing a comma:
%div{class: 'g-recaptcha', 'data-sitekey' => 'your site key'}
data-sitekey is a html5 data attribute.
html2haml can convert HTML to haml.
In my application I have places where users can send messages and leave comments on content. So to keep this simple, I have Comment, with and id (integer) and body (text).
When printing these in HTML, I want the user's formatting, for example a comment might be entered like this:
comment added
with an enter
In the database, this comes to be:
comment.body.last
"comment added\r\n\r\nwith an enter"
On my page then, this comes to simple look like "comment added with an enter". What I'm wondering is if there is a way in my view to let it know I want to render these enter characters? As 's or something? Does anyone know how to preserve the user's format?
Thanks a ton guys.
simple_format will format text in the way you are describing. It's built in to Rails.
Use simple_format: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
<%= simple_format(your_string_variable) %>
on mysite I have the ability to add comments. Sometimes users enter comments will links (href. ...)
I would like to have those links to be clickable/linkable (a href) when the comment is displayed to users.
how with Rails 3 can I take a comment, and look for links and then wrap those links in an a href tag that opens in a new window?
Thanks
The simplest way is to use the auto_link method built into rails.
Note: In Rails 3.1 auto_link has been moved into a separate gem.
idlefinger's suggestion of #auto_link is perfect. I know it's not the question you originally posed, but wanted to suggest: also check out #simple_format, which will nicely format your users' use of newlines into br and p tags.
I need to embed a flash movie into one of the pages in my Ruby on Rails app. I've put the Flash movie into the public folder, but I'm not sure how to reference it from my page, which is located at views/controller_name/page1.html.erb. How do I do this?
Thanks for reading.
link_to, eg (assuming it's in the public folder):
<%= link_to "My Hot Link Test", "/flashmovie.swf" %>
The leading "/" isn't strictly necessary, I think.
Sorry, missed the bit where you wanted to embed it, not link to it. Contrary to the title of this question. ;-) This is one easy way:
http://agilewebdevelopment.com/plugins/flashobject
we have done this, But we have just use plan html
say your flash banner is at
/public/flash/ folder
this is nothing but plain html
If someone have a better option please let me know too
cheers
sameera
I have a rails application where I allow users to enter comments. The comments are displayed back like so
<%= simple_format(sanitize(c.comment)) %>
If a user enters the following the in the comment link this link gets appended to the end of the rails root directory. So if someone clicked on the link the would go to
www.somedomain.com/myrailsapp/www.blah.com
What can I do to correct this?
Thanks
You will need to append "http://" in the href attribute of the anchor tags.
And if you aren't using it, may I suggest the auto_link helper method. It will automatically do what you are looking for.