Rails auto_html gem 帮助 - 不保存为 html

发布于 2024-11-05 19:05:21 字数 2589 浏览 0 评论 0原文

我正在尝试创建在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-11-12 19:05:21

您正在使用额外的后缀 _html

输出上使用 *body_html*

您基本上需要更改为:

auto_html_for :body

在输入上使用 body (并在模型中声明 auto_html_for)并在 :

...
<%= f.text_area :body, ...

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, ...
日暮斜阳 2024-11-12 19:05:21

对于你的问题,将其保存为 html。
你可以尝试这个:

def body_html
auto_html(self[:body_html]) { simple_format; link(:target => 'blank') }
end

或者

 auto_html self[:body_html] do
    html_escape
    image
    youtube(:width => 400, :height => 250)
    link :target => "_blank", :rel => "nofollow"
    simple_format
  end
  end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文