Rails auto_html gem help - Not saving as html
I am trying to create form that uses the auto_html gem on the text_area body_html. I have follow this guide how to create the auto_html on a text_area.
I have that problem that the preview dont gets rendered without a browser refresh and the text_area body_html gets not saved as html code just as plain text.
My model:
class Post < ActiveRecord::Base
auto_html_for :body_html do
html_escape
image
youtube(:width => 400, :height => 250)
link :target => "_blank开发者_StackOverflow中文版", :rel => "nofollow"
simple_format
end
end
My controller:
include AutoHtml
# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
end
My new form:
<script type='text/javascript'>
function load(request) {
$('#code').text(request);
$('#preview').html(request);
}
function preview(value) {
$.getJSON("http://auto_html.rors.org/comments/preview?callback=?", value, function(data){
load(data);
});
}
function previewComment() {
preview({'t':$('#comment_body').val()});
}
$('#examples a').click(function() {
$('#comment_body').focus();
$('#comment_body').val( $(this).attr('href'));
previewComment();
});
$(function () {
$("#comment_body").focus();
previewComment();
});
$('#comment_form').delayedObserver(1, function(element, value) { previewComment() })
</script>
<h1>New post</h1>
<form id="comment_form" action="/posts" method="post">
<h3>Type or paste URLs</h3>
<%= simple_form_for @post do |f| %>
<%= f.input :titel, :label => 'Titel', :style => 'width:500;' %>
<%= f.text_area :body_html, :id => 'comment_body', :label => '125x125', :style => 'width:500;' %>
<%= f.button :submit, :value => 'Create post' %>
<% end %>
<h3>Code</h3>
<div id="code"></div>
<h3>Preview</h3>
<div id="preview"></div>
</form>
<%= link_to 'Back', posts_path %>
You're using extra sufix _html
Use body on inputs (and declaring auto_html_for in model) and *body_html* on outputs
You basically need to change to this:
auto_html_for :body
And in form:
...
<%= f.text_area :body, ...
For your problem saved it as html. You can try this:
def body_html
auto_html(self[:body_html]) { simple_format; link(:target => 'blank') }
end
or
auto_html self[:body_html] do
html_escape
image
youtube(:width => 400, :height => 250)
link :target => "_blank", :rel => "nofollow"
simple_format
end
end
精彩评论