Rails auto_html gem 帮助 - 不保存为 html
我正在尝试创建在 text_area body_html 上使用 auto_html gem 的表单。我已按照本指南如何在 text_area 上创建 auto_html 进行操作。
我遇到了预览无法解决的问题无需刷新浏览器即可呈现,并且 text_area body_html 不会像纯文本一样保存为 html 代码。
我的模型:
class Post < ActiveRecord::Base
auto_html_for :body_html do
html_escape
image
youtube(:width => 400, :height => 250)
link :target => "_blank", :rel => "nofollow"
simple_format
end
end
我的控制器:
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
我的新形式:
<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 %>
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", :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 %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用额外的后缀 _html
输出上使用 *body_html*
您基本上需要更改为:
在输入上使用 body (并在模型中声明 auto_html_for)并在 :
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:
And in form:
对于你的问题,将其保存为 html。
你可以尝试这个:
或者
For your problem saved it as html.
You can try this:
or