How can I get RoR to display html pulled directly from a database,
without converting the tags. (e.g. if “
Hello
” is stored (as
text) in a database, how can I get RoR NOT to corvert it to
“<h1>Hello”, but instead to just pass the tags along so that
“Hello” displays in bold.
I’m sure it’s just a simple method call, but I’ve been poking around and
I can’t find anything.
Thanks,
As far as I know, there shouldn’t be any problem doing this; Rails
should do it by default. Unless you are calling h(text) before saving
to the database or in your view, characters like < or > should not be
converted to HTML entities.
The question is, where did this HTML in your database come from? A text
area in one of the forms in your app, or somewhere else? If it was
saved directly from a text area, you shouldn’t be having a problem…
So far I’ve been manually entering the html (for testing purposes)
through the text areas on the scaffold-generated forms. So, I take it
this shouldn’t be a problem.
But you raise a good point. It hadn’t occured to me that actual data in
the db might have the html entities instead of the desired ‘<’
characters. Is there an easy way for me to check what’s actually in
the db?
Eleo wrote:
As far as I know, there shouldn’t be any problem doing this; Rails
should do it by default. Unless you are calling h(text) before saving
to the database or in your view, characters like < or > should not be
converted to HTML entities.
The question is, where did this HTML in your database come from? A text
area in one of the forms in your app, or somewhere else? If it was
saved directly from a text area, you shouldn’t be having a problem…
I check the data in the db (using the data browser on MySQL-front) and
it looks fine – no html entities.
But still, the data is being converted: the final html file generated
from the db, through the views, etc., contains the entities and not the
plain html that I want to display.
???
Alex A. wrote:
So far I’ve been manually entering the html (for testing purposes)
through the text areas on the scaffold-generated forms. So, I take it
this shouldn’t be a problem.
But you raise a good point. It hadn’t occured to me that actual data in
the db might have the html entities instead of the desired ‘<’
characters. Is there an easy way for me to check what’s actually in
the db?
Eleo wrote:
As far as I know, there shouldn’t be any problem doing this; Rails
should do it by default. Unless you are calling h(text) before saving
to the database or in your view, characters like < or > should not be
converted to HTML entities.
The question is, where did this HTML in your database come from? A text
area in one of the forms in your app, or somewhere else? If it was
saved directly from a text area, you shouldn’t be having a problem…
The data looks solid: no html entities.
???
James L. wrote:
On 7/18/06, Alex A. [email protected] wrote:
So far I’ve been manually entering the html (for testing purposes)
through the text areas on the scaffold-generated forms. So, I take it
this shouldn’t be a problem.
But you raise a good point. It hadn’t occured to me that actual data in
the db might have the html entities instead of the desired ‘<’
characters. Is there an easy way for me to check what’s actually in
the db?
Most every database has a client program, or there are dozens of
different thrid-party options available. Check your database
documentation.
On 7/18/06, Alex A. [email protected] wrote:
So far I’ve been manually entering the html (for testing purposes)
through the text areas on the scaffold-generated forms. So, I take it
this shouldn’t be a problem.
But you raise a good point. It hadn’t occured to me that actual data in
the db might have the html entities instead of the desired ‘<’
characters. Is there an easy way for me to check what’s actually in
the db?
Most every database has a client program, or there are dozens of
different thrid-party options available. Check your database
documentation.
On 7/18/06, Alex A. [email protected] wrote:
I check the data in the db (using the data browser on MySQL-front) and
it looks fine – no html entities.
But still, the data is being converted: the final html file generated
from the db, through the views, etc., contains the entities and not the
plain html that I want to display.
We’re probably going to need to see some code .
On 7/18/06, Alex A. [email protected] wrote:
Uh-oh. I don’t know that I’m rendering at all. For these tests, I’ve
just been using the scaffold-generated code. I’ve made two small
modifications, which I can’t imagine would effect this.
Eleo already predicted this in his first reply to your question.
You’re calling h(), which is doing exactly what you’re seeing to your
HTML.
<td><%=h problem.send(column.name) %></td>
^^^^
– James
How exactly are you rendering it?
Uh-oh. I don’t know that I’m rendering at all. For these tests, I’ve
just been using the scaffold-generated code. I’ve made two small
modifications, which I can’t imagine would effect this.
- I’ve tried to set the MIME-type as application/xhtml+xml
(in application.rb, I’ve added:
after_filter :set_charset
def set_charset
@headers[“Content-Type”] ||= “application/xhtml+xml;
charset=iso-8859-1”
end
)
- I’ve added some extra junk to the layout view, including some MathML,
which displays fine.
–
Anyway, the (predictable) code,
controller:
class ProblemsController < ApplicationController
def index
list
render :action => ‘list’
end
view:
<% for problem in @problems %>
<% for column in Problem.content_columns %>
<%=h problem.send(column.name) %>
<% end %>
<%= link_to 'Show', :action => 'show', :id => problem %>
<%= link_to 'Edit', :action => 'edit', :id => problem %>
<%= link_to 'Destroy', { :action => 'destroy', :id => problem },
:confirm => 'Are you sure?', :post => true %>
<% end %>
–
James L. wrote:
On 7/18/06, Alex A. [email protected] wrote:
I check the data in the db (using the data browser on MySQL-front) and
it looks fine – no html entities.
But still, the data is being converted: the final html file generated
from the db, through the views, etc., contains the entities and not the
plain html that I want to display.
We’re probably going to need to see some code .