Rails auto_html gem help - 不保存为html

时间:2011-05-07 11:20:53

标签: jquery ruby-on-rails ruby ruby-on-rails-3 gem

我正在尝试创建在text_area body_html上使用auto_html gem的表单。我已按照本指南how to create the auto_html on a text_area.

我遇到的问题是,如果没有浏览器刷新,预览就不会被渲染,而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 %>

2 个答案:

答案 0 :(得分:2)

你正在使用额外的sufix _ html

在输入上使用 body (并在模型中声明auto_html_for)和输出上的* body_html *

您基本上需要更改为:

auto_html_for :body

以形式:

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

答案 1 :(得分:0)

对于你的问题,将其保存为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