I've just included the ckeditor in my rails app; works great. I have a PageComponent model which has a text field that stores the contents that are then rendered into the show view,开发者_Go百科 like this:
<%= raw @page_component.page_content %>
I just had the idea that maybe I could include an erb statement within the page_content attribute text, like this:
<%= render :partial=>'some_partial' %>
but it doesn't appear to work. The text is saved properly into the field, but nothing appears on the page. This was just a basic idea test, and I'm not sure it's even possible as rails may have already gone through the whole response rendering thing at this point. Is doing something like this possible, and if so, how?
Like you said, the <%= %>
tags are ERB tags, so one way of solving your problem is to simply use the ERB class provided in Ruby (and already included in your views and helpers) to translate and execute the contents of any tags in the the page_content
string and give you the resulting string back. The binding()
parameter passed to the result method contains the current (Rails) context, allowing the string to be interpreted just like a normal Rails view.
You could put the following method in your PageComponent helper:
def translate_erb(string)
ERB.new(string, 0).result(binding()) # 0 is a 'safety level' (see docs)
end
Then in your view, call that method like so:
<%= raw translate_erb(@page_component.page_content) %>
Whilst this will work, I'd highly recommend against it. By implementing this, you are allowing the user a lot more flexibility than they should ever have and, as a result, this is a complete security disaster. Ideally you should create your own markup or use some other method to add this functionality in such a way that you don't give the user total freedom to do whatever they want. However, as long as you understand this, it is a viable method. Just please be very careful how you use it!
精彩评论