Rails render - get partial source instead of output HTML - ruby-on-rails

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>

Related

How to use slim for mailer text template

I am using slim as the template engine for my rails app and would like to use slim for mailer templates as well.
There is no problem with html mailer templates (views/mailer/default_email.en.html.slim) but, I am not sure how to make the text templates work.
I have placed a text template in views/mailer/default_email.en.text.slim with this content:
Hello,
Your video is ready.
= #url
Thank you,
The A-Team
But the result is parsed as slim HTML, and looks like this:
<Hello>,</Hello><Your>video is ready.</Your><Click>the link below to watch it:</Click>http://watch.thy/video<Thank>you,</Thank><The>A-Team</The>
Other than prefixing every line with a pipe, isnt there a more natural way?
I even looked for an embedded plugin (like the markdown one) to say "plain text" but there is none.
Thanks in advance.
Slim was designed to generate HTML, not plain text, so you'll have to either use the pipe prefix for each line or go with .text.erb templates. I'd use the ERB templates, especially if you don't have a lot of interpolation going on.
For what it's worth, you can actually go the Slim route without having to prefix each line with a pipe, like so:
|
Hello,
<br><br>
Your video is ready.<br>
#{#url}
<br><br>
Thank you,<br>
The A-Team
I would definitely agree with eugen though, that the text.erb route is the best fit. Just providing another solution, in case somebody absolutely insisted on doing this in Slim. :-)

How to find where a view partial is used?

How do you go about easily finding out where a Rails view partial is used?
In what views, controllers etc.
This is handy when working on an app that someone else wrote. You don't necessarily know what views are using a particular partial, or where to find where the partial is used when navigating the app in the browser.
Currently, I am using the Sublime Text editor to project-wide search for the partial name "form" or "_form", or for "render ", but this gives an unnecessary amount of useless results.
You could try putting this caller inside the partial, and then running your test suite:
#haml
- p caller[x]
#erb
<%- p caller[x] %>
I used x because you'll have to play around with which index you're calling to get useful information.
There's no built-in solution, but I wrote a gem to try to solve this exact problem: https://github.com/Negotiatus/Partial-Finder
It's a rake task that will recurse through your project and attempt to create the full render chain (and routes!) for a given partial. Hope it helps!

How would I implement google's new reCaptcha in Haml?

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.

trouble using angularjs with slim in a rails project

I've finally decided to give angularjs a whirl and i'm running into some early trouble.
I'm using Rails 3.2 and the Slim template gem for the view.
I'm just trying the example from the angularjs site here: http://angularjs.org/#todo-html
Here's the relevant bit:
<div ng-controller="TodoCtrl">
<span>{{remaining()}} of {{todos.length}} remaining</span>
Which in slim would be something like:
div(ng-controller="TodoCtrl")
span {{remaining()}} of {{todos.length}} remaining
The problem is Slim just prints:
{{remaining()}} of {{todos.length}} remaining
literally as a string.
Anyone able to get Slim and Angular to play together?
I finally got it to work.
I had to go into my layout and do this:
html(ng-app='')
You can probably add that to a div on the particular page also.
div(ng-app='')
div(ng-controller="TodoCtrl")
span {{remaining()}} of {{todos.length}} remaining
Hopefully this helps someone. It took me a bit to figure out.
You can also do it like so:
html [ng-app]
Or:
div [ng-app]
div [ng-controller="TodoCtrl"]
span {{remaining()}} of {{todos.length}} remaining

rails 3 is printing html tags to screen instead of rendering them

I'm using vhochstein's fork of active_scaffold, which runs quite nicely on rails 3, except for a few small bugs - http://github.com/vhochstein/active_scaffold.
In rails 2.3, the following code disables a link:
return "<a class='disabled'>#{text}</a>" unless authorized
But in Rails 3, it causes the escaped html tags to be printed out instead as in the following photo:
How can I make the content of this return statement render the way it should in rails 3?
The code above, is from the list_column_helpers.rb file in vendor/plugins/active_scaffold/helpers/
UPDATE:
Floatless fixed this by suggesting to add .html_safe to the code.
I have since found that the folowing change also needs to be made as there's more than one bit of code that is respondible for disabling action links in active_Scaffold:
In /plugins/active_scaffold/frontends/default/views/_list_actions.html.erb change:
<%= record.authorized_for?(:crud_type => etc etc etc -%>
By making it use "raw"
i.e.
<%= raw record.authorized_for?(:crud_type => etc etc etc -%>
Anyway, thanks to floatless and hopefully mr hochstein will be able to use this stuff.
Try this:
return "<a class='disabled'>#{text}</a>".html_safe unless authorized

Resources