Ranzit
Ranzit

Reputation: 1347

Rails - use code(css, js, html) from server on client side

I had a application rails built with bootstrap and simple form. Here I have to show the UI patterns how they actually look like. That means I have to show the patterns like menu bar, accordian patterns examples in my application. For that I am storing the pattern code html,css,js in database.

Here my requirement is I have to show the actual code pattern view from the stored record(css,js,html) without any css/js conflicts.

How can eneter the html,css,js code dynamically in a partial or page to show that in a fancybox in rails.

Thanks for the help in advance.

Upvotes: 0

Views: 146

Answers (1)

Brian
Brian

Reputation: 31282

just use html_safe or raw to render your content as a normal string on views. For instance:

in your controller:

@x = YOUR_CODE_FROM_DB

in your view:

<%= @x.html_safe %>
# <%= raw @x %> is also ok in this case

NOTICE: you can use html_safe on models, but raw is declared on a helper, so you can just use it on controllers and views.

-- edit --

more example:

on controller:

@hello = 'alert("hi");'
@body = 'body{ background: red; }'

on view:

<script type="text/javascript" charset="utf-8">
  <%= raw @hello %>
</script>

<style type="text/css" media="all">
  <%= @body.html_safe %>
</style>

Upvotes: 4

Related Questions