Using reCaptcha with Rails 2.3.12 - ruby-on-rails

So I'm trying to get reCaptcha to render on a partial form view that uses HAML. I have tried using the :ruby filter and then adding <%= recaptcha_tags %> but that didn't work, neither has anything else that I've found. Is there a way to implement this?
*Revision
Ahem, more specifically, can anyone tell me what I need to have for the <%= recaptcha_tags %> helper? Every thing I find on this subject just says "Add <%= recaptcha_tags %> wherever you want it to appear!" and absolutely nothing on what the helper should contain.
*Another Revision
I am indeed trying to use Ambethia. I tried using just = recaptcha_tags but that didn't work, I got an error saying it was an undefined variable or method. I installed the Ambethia/reCaptcha as a plugin using script/plugin install git://github.com/ambethia/recaptcha.git and I put config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com" in environment.rb along with my public/private keys.
*Started Over
Okay, got rid of everything I had done initially. Can anyone help me with this? I follow all of the tutorials I can find on it, but none of them explain how to implement/create the helpers for <%= recaptcha_tags %> or <%= verify_recaptcha %>. I'm obviously new to RoR and implementing reCaptcha of any kind, so I'm sorry I'm asking for my hand to be held but I am honestly lost and am not finding any guidance anywhere! Thanks so much anyone and everyone.

did you try simply:
= recaptcha_tags
You don't mention the plugin you're using. I'm assuming this one. If that's the case, the recaptcha_tags helper will return the HTML for the captcha, and you'd insert it into whichever forms you wanted the captcha to appear on.
The <%= %> around recaptcha_helper aren't part of the helper, but rather the way you insert content into erb templates (and other templating languages resembling erb). In Haml you don't need the surrounding tag. It's just =.

I had the same problem and I finally solved it realizing that my form was called asynchronously.
I added:
= recaptcha_tags :ajax => true
and captcha appeared.
Hope this could help the original question.

Related

Adding hyperlink in mail

My requirement is to add a link to mail sent from application developed in Ruby On Rails. Clicking that link in mail need to route user to that particular record in the application.
Can someone please help me with this some sample code.
In your view template use _url. E.g.:
<%= link_to 'Edit User', edit_user_url(#user) %>
it will return the complete URL.
You can use a simple link_to method in the mail or you could use a simple anchor tag. The latter you would have to build up.
If you need information on how to build links and paths check the rails guides. This has an example on the first section of the guide.
e.g. <%= link_to 'Patient Record', patient_path(#patient) %>
Besides this you need to provide more information if you want more help.
If you are starting with Ruby On Rails I would recommend looking at the videos on rails cast.
For your email problem you can look at sending-html-email
Also in the email I like to use the link_to helper like this
<%= link_to 'Click Me', something_url(#user.id), %>
I hope that this works. And Happy Coding

Ruby on Rails: bbc-code and performance

I am using bb-code in a Rails application for postings and comments. At the moment, I have the following to put a post's content in a view:
<%= #post.content.bbcode_to_html.html_safe.gsub('<a', '<a rel="nofollow"') %>
What is the best way to convert the bb-code to html and add "nofollow" to all links?
Thanks!
The bb-ruby gem you are using allows for using custom BBCode translations passed as parameters to the bbcode_to_html method. However, if you really want ALL links to contain the rel="nofollow", I think your best bet is going to be monkey patching them gem itself. Based on the BBRuby source, you want to do this:
module BBRuby
##tags = ##tags.merge({
'Link' => [
/\[url=(.*?)\](.*?)\[\/url\]/mi,
'\2',
'Hyperlink to somewhere else',
'Maybe try looking on [url=http://google.com]Google[/url]?',
:link],
'Link (Implied)' => [
/\[url\](.*?)\[\/url\]/mi,
'\1',
'Hyperlink (implied)',
"Maybe try looking on [url]http://google.com[/url]",
:link],
'Link (Automatic)' => [
/(\A|\s)((https?:\/\/|www\.)[^\s<]+)/,
' \2',
'Hyperlink (automatic)',
'Maybe try looking on http://www.google.com',
:link]
})
end
This will rewrite the BBRuby translator to always include a nofollow attribute. I would put this in config/initializers with a descriptive filename such as bbruby_nofollow_monkeypatch.rb
As for the html_safe, I would leave that as is. As I understand it that is a preferred way of doing it and in my opinion it keeps your intent clear. The above monkey patch makes the line in your view more readable:
<%= #post.content.bbcode_to_html.html_safe %>

rails active_link_to gem functioning on params

I'm using the Rails gem active_link_to for determining the active URL for menus. It works great on straight forward links such as '/controller/action' but how can I do it similarly for particular param?
Ie /controller/action?user=all
Or is there a better gem that can provide this?
https://github.com/twg/active_link_to
You could use the Regex functionality of the gem, depending on how your code is organized.
<%= active_link_to("Link Text", some_place_path, :active => /#{Regexp.escape(#param_value)}/, class: "blogYear") %>
You can execute code within a Regex with the above method if you are going to output the param or param value dynamically.

render all passed parameters quickly in rails

I have a form that populates various values, then posts those values to a page when the user submits. Now, I wish to see what params[] contains (by displaying it on the page I post to) just to fool around with my form definitions a little bit. I looked for an easy way to render params, but haven't quite found the solution. Any helpful suggestions SO?
thanks in advance
Another way is to make use of the helper functions for debugging as described in http://guides.rubyonrails.org/debugging_rails_applications.html
The technique, as previously described is to use params.to_yaml.
Alternatively, in your application.html.erb file put the code
<%= debug(params) if Rails.env.development? %>
after the <%= yield %> call
This will display in the view the params, for example
--- !map:ActiveSupport::HashWithIndifferentAccess
action: edit
controller: contracts
id: "8"
The nice feature is that the information is only output in the Development mode environment, as per the check that is executed to determine which Rails environment is running.
Try <%= params.to_yaml %> in your view
Try using in this view in the view you are posting
<%= params[:name_of_posted_param].each {|param|
params //do something with the param here
} %>
A better way would be to do this in the helper and pass the returning variable in the view.
-R

Implementing sanitize simple_format in rails 2.3.8

I have created an application that allows for users to input lots of different data (posts, comments, etc.). The simple_format is good for me for now I just want to protect against crazy stuff. I haven't used sanitize before and after reading some guides I am still a little confused on how to implement. Hoping I can get some direction here.
Let's say I am collecting #post.body. How do I remove any <div> tags or <script> tags that might be entered by the user? I am assuming that in the view it would look something like this:
<%= sanatize(simple_format #post.body) %>
...but where do I define what tags aren't allowed? In the Post model or in a sanitize_helper? What is the correct syntax here?
Here's the documentation link for the sanitize method in Rails 2.3.8. With that in mind you'll be able to define allowed tags in this way:
<%= sanitize(simple_format(#post.body), :tags => %w(p span strong)) %>
Note that you can define them also inside the Rails Initializer:
Rails::Initializer.run do |config|
config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
end
I hope you find this helpful!

Resources