BBCode for Ruby on Rails - ruby-on-rails

So I'm putting together a simple forum. I'd like to allow my users limited formatting options and BBCode would be plenty for my users. Knowing that I'm assuredly not the first one to want to use BBCode with RoR I googled but couldn't find a straight forward tutorial on how to create a editor which accepts BBCode nor a way to parse and display BBCode formatted input.
Any help or guides would be appreciated!

You should give bb-ruby a try. Its documentation on the web page seems to be very clear and straightforward.

Here is another gem you may find useful
http://github.com/jarrett/rbbcode

Gemfile
gem 'bb-ruby'
# run `bundle`
In the place (haml):
%h1= put_header_string.bbcode_to_html.html_safe
%p= "[b]bold text[/b]".bbcode_to_html.html_safe
Besides a builtins you could also extend your own bbcode as you need. For example:
module BBRuby
##tags = ##tags.merge({
'Email' => [
/\[email(:.*)?\](.*?)\[\/file\1?\]/mi,
lambda{ |e| "<span class='email'>#{e[2].gsub('#','<i>(at)</i>')}</span>"},
'protect email from spam',
'[email]electronic#test.ru[/email]',
:email
],
})
end
In place
[b]Contact me:[/b][email]email#test.ru[/email]
Contact me: email(at)test.ru
bb-ruby on github | bb-ruby on rubygems | bb-ruby home | tags processed list

Related

Replace string with substring that is html safe [duplicate]

I have a text area into which users enter a lot of text that potentially includes hyperlinks. How can I display the text and have the URLs automatically appear as hyperlinks? Is there a gem, plugin, or existing method that does this? I'm looking to be able to do something like:
<%=h #my_object.description.with_links %> in my view.
Rails 3
Use the provided helper method called auto_link, which is part of ActionPack.
<%=h auto_link(#my_object.description) %>
Rails 4
Auto-linking has been removed from Rails and has been made into the rails_autolink gem.
require 'rails_autolink'
auto_link("Go to http://www.rubyonrails.org and say hello to david#loudthinking.com")
# => "Go to http://www.rubyonrails.org and
# say hello to david#loudthinking.com"
See rinku gem, which is apparently faster and more accurate than autolink gem

Markdown gem for Rails

What is one of the best gems out there to implement markdown in user posts?
I am currently looking to allow users to edit their text via profiles, discussions, etc. and the simple_format solution is no longer suitable for me.
RedCarpet is the Ruby community's usual go-to choice for implementing Markdown, though there are other options (Kramdown, for example).
To render your users' posts as Markdown with RedCarpet, you would write:
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true)
markdown.render(#post.content)
With Kramdown, the API is a bit simpler:
Kramdown::Document.new(#post.content).to_html
RedCloth does support a wealth of configuration options, though, which Kramdown does not.
redcarpet + markitup = example

Rails 3: Working Mongoid captcha exists?

I've tried a handful of captchas for Rails 3 and none tend to play nicely with Mongoid. I don't need anything too fancy just something to do a quick human check.
How do you guys get one working with Mongoid? Are there alternative solutions?
That's outside mongoid scope, but still applicable. Have a look at Negative Captcha:
Negative captchas create a form that has tasks that only bots can perform, but humans cannot. This has the exact same effect, with (anecdotally) a much lower false positive identification rate when compared with positive captchas. All of this comes without making humans go through any extra trouble to submit the form. It really is win-win.
You can use simple-captcha v1rtual's branch with mongo support. Simple and clean setup and usage:
Just add to your Gemfile as:
gem 'wolcanus-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/v1rtual/simple-captcha.git'
Run generator:
rails generate simple_captcha
For Controller Based, add the following line in the file "app/controllers/application.rb":
ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers
end
In the view file within the form tags add this code:
<%= show_simple_captcha %>
and in the controller’s action authenticate it as
if simple_captcha_valid?
do this
else
do that
end
See the branch for more options: https://github.com/v1rtual/simple-captcha

Is there any way to display Rails' log files in the browser?

This might be (read: probably is) a dumb question, but here goes...
Is there a simple, preferably non third-party, way to display Rails logs in the browser? It gets kind of old opening a log file, then closing it and re-opening it on the next error. It would be superfantastic to just to go domain.tld/logs, which would require user authentication, of course.
For reference, there is a very simple way to do this. However, you would want to protect this page with some sort of authentication/authorization.
Controller:
lines = params[:lines]
if Rails.env == "production"
#logs = `tail -n #{lines} log/production.log`
else
#logs = `tail -n #{lines} log/development.log`
end
Log File View:
<pre><%= #logs %></pre>
View to display link:
<%= link_to "log file", log_path(lines: 100) %>
Routes:
get 'logs/:lines' => "pages#log", as: "log"
All you need is to open log file and put its content into browser.
Realted topic: ruby: all ways to read from file.
Also you should know that your logs grow very fast, and it's not a good idea to show whole log in browser. You can just open last 100-200 rows. Related topic: Reading the last n lines of a file in Ruby?
Also you can try this solution: http://newrelic.com/. It is more complex and little oftopic, but quite useful.
I created a for this purpose called browserlog.
Installing is just a matter of adding the gem to the Gemfile:
gem 'browserlog'
Afterwards all you need is to mount the engine on the route you want:
MyApp::Application.routes.draw do
mount Browserlog::Engine => '/logs'
end
Once that's set up, accessing /logs/development (or production, test, etc) will show a window like this:
(source: andredieb.com)
Pimp my Log can be used to visualization many types of logs including Ruby on Rails.
It also includes the management of users and notification.
[Edited]
By default, the PimpMyLog does not support Ruby on Rails logs. It's necessary to implement the regex to works with it.
[/Edited]

How to use Rails I18n.t to translate an ActiveRecord attribute?

Trying to use my active record translations in config/locales/de.yml also in my views. I thought I am clever using this:
de:
activerecord:
attributes:
user:
login: "Benutzerkennung"
comment: "Bemerkungen"
And in my view this:
<%= label_tag :login, t('activerecord.attributes.user.login') %>
But instead of the translation value ("Benutzerkennung") I am getting the infamous
"translation missing: de, activerecord, attributes, user, login"
Has anybody got this working (not using the label translation plugin (I am wary of potential side effects), or User.humanize_attribute_name)? What am I missing? (it does work when I use "activerecord1" or something else than activerecord, so my setup seems to be fine)
Thanks!
Ok, my bad, it does work just fine. I fell in the YML formatting trap :(
To help you debug along the way, use "script/console" and the following statements:
- I18n.locale --> should output the locale you want to examine
- I18n.t('activerecord.attributes') --> should give you all the key/value pairs for your translation, if not, you made a formatting error in your YML file or it could not be found
And btw - the plugin works quite well http://github.com/iain/i18n_label/
if you don't like the result of ".human_name" (which the plugin uses), just fall back to I18n.t('your key')
Another method:
<%= label_tag :login, User.human_attribute_name(:login) %>
You should upgrade Rails gem to v2.3.11 (I tried to use 2.3.9, but now days it is not available so I suggest you 2.3.11)!
gem install -v=2.3.11 rails
You can find this issues documented here: Rails 2.3.9 Release notes

Resources