How would I implement google's new reCaptcha in Haml? - ruby-on-rails

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.

Related

Render images into my post content

I'm trying to add images to my post which I thought that would be easier but god I've been struggling for a while now.
I tried to use Redcarpet and added some ruby Image_tag code but didn't work. Also I tried with tag and the raw method but no luck. I don't know I've been going around but I couldn't fine anything and I think I'm not asking nothing weird or complicated ahah.
Just add pictures from my /assets/images folder if is possible.
<%= img_tag ("my_photo.jpg")%>
How do you recommend to do it?
Many thanks
I use the image_tag helper and this has always worked fine for me. You could try this:
<%= image_tag("my_photo.jpg")%>
I figured it out. I used HTML tag instead of Ruby code.
<%= raw (#post.content) %>
As I am using assets pipeline:
<img src="/assets/my_photo.jpg" alt="twd" class="img-rounded"></img>
My issue was that I was calling my photo as /images/my_photo.jpg instead of /assets/my_photo.jpg... simple as that

Rails render - get partial source instead of output HTML

I don't really have a big issue and my bad if it's obvious but I couldn't find this on SO or google which is quite rare, anyways... I'm using Ruby on Rails to create a pattern library which ofcourse contains code snippets to go along with examples, now what I did was create a partial in a folder somewhere in my views - now it's no problem to render this partial using:
= render partial: '/snippets/grid/single-column.html.slim'
However, for these snippets I would also want to render the actual source of the slim file itself e.g.
.container
ul.test
li: a href="#"
li: a href="#"
li: a href="#"
li: a href="#"
I would like the output of the 'plain' render to be as the above snippet.
The reason for doing this is that I'm using Google Code Prettify to beautify this code for readability and since we're using slim as a templating engine here it's easy to just copy the snippet and paste it into a view anyways.
I'm not sure wether or not this is possible or if this type of rendering has a specific name - If so please do tell :)
For reference I looked in the following places to see if I could get a grip on this:
github - rails render
apidock - rails render
slim-lang - verbatim text
However I did not find my answer here.
Furthermore I'm a freshman when it comes to rails so I haven't got alot of experience yet in building these kinds of functions (it's nice to understand the resources you're working with ;)).
As usual, all help is appriciated.
Thanks in advance - Sidney Liebrand
EDIT
Yeah, what might also be smart is to include rails / slim versions:
Rails 4.2.1
Slim 3.0.1
You have to read the content of the file like a string in the controller for example:
#code = File.open('app/views/snippets/grid/single-column.html.slim').read
And in the view something like this:
<code class="prettyprint"><%= #code %></code>

Rails 4 WYSIWYG Bootsy not displaying formatting

I've just followed the install instructions at the the bootsy gem page and it all looks good until I save and look at the post's content.
This is what the output looks like in the view:
<h2>Header</h2><h3>Sub head:</h3><br><img alt="Thumb 1320236280147" src="/uploads/bootsy/image/1/1320236280147.jpg">
It seems that none of the html formatting is being rendered as it is being escaped by quoation marks or something like that - has anyone else had this problem? I haven't seen any issues on the github page or on SO to point me in the right direction.
I haven't done anything yet apart from follow the gem install instructions but maybe I missed something or am just making a stupid mistake.
If there's anything else you want to know please just ask.
Cheers
You need to have something like this, escape html:
<%= f.bootsy_area(:body, class: 'bg-code').html_safe %>
Sergio is correct but have the .html_safe on the output rather than the input.
For example, on a post index:
<%= post.body.html_safe %>
Hope this helps.
maybe use raw?
<%= raw(post.body) %>
You can also add to bootsy.rb
config.editor_options = {
html: true
.....
}
to enable you to toggle html in the bootsy editor .

Rails 3 - Comments/Message - "\n\r"

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) %>

RoR - make links clickable that show up in a user comment

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.

Resources