Rails render - get partial source instead of output HTML

1.1k views Asked by At

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:

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
1

There are 1 answers

3
gabrimac On BEST ANSWER

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>