开发者

Rails help to preview text area

开发者 https://www.devze.com 2023-03-04 03:28 出处:网络
I am trying to create a preview for my text area: I have tried this: My controller: def preview post = Post.new(params[:post])

I am trying to create a preview for my text area:

I have tried this:

My controller:

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

And my form:

<%= 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 %>

How should I make a preview in my form of the body_html text area? I want to have a preview when a type something in the body_html text_area it gets previewed instant.


Is the user entering HTML that you want rendered in the preview? Or just the text as is?

I guess either way, add a div for the preview:

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

and then using jQuery:

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

精彩评论

暂无评论...
验证码 换一张
取 消