【问题标题】:Rails help to preview text areaRails 帮助预览文本区域
【发布时间】:2011-05-07 08:21:11
【问题描述】:

我正在尝试为我的文本区域创建预览:

我试过这个:

我的控制器:

def preview
  post = Post.new(params[:post])
  render :text => post.body_html
end

def new
  @post = Post.new

  respond_to do |format|
    format.html # new.html.erb
    format.xml  { render :xml => @post }
  end
end

还有我的表格:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
        <% @post.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :titel %><br />
    <%= f.text_field :titel %>
  </div>
  <div class="field">
    <%= f.label :body_html %><br />
    <%= f.text_area :body_html %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我应该如何在 body_html 文本区域的表单中进行预览? 我想在 body_html text_area 中输入内容时进行预览,它会立即预览。

【问题讨论】:

  • 这只是关于 javascript

标签: javascript ruby-on-rails ruby ajax ruby-on-rails-3


【解决方案1】:

用户是否输入了您希望在预览中呈现的 HTML?还是只是原样的文字?

我猜无论哪种方式,为预览添加一个 div:

<div id='preview'></div>

然后使用jQuery:

var $html = $('#body_html');
$html.bind('change keyup',function(){
   $('#preview').html($html.val());
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    相关资源
    最近更新 更多