2011-05-28 79 views
-2

我正在嘗試預覽文本區域。但它dosnt工作。Rails幫助預覽文本字段

我new.html.erb:

<h1>New post</h1> 

<%= render 'form' %> 

<%= link_to 'Back', posts_path %> 

我form.html.erb:

<%= 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="field"> 
    <%= f.label :up_votes %><br /> 
    <%= f.text_field :up_votes %> 
    </div> 
    <div class="field"> 
    <%= f.label :down_votes %><br /> 
    <%= f.text_field :down_votes %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
<div id="post-preview" style="width:400px;border:1px solid black; height:500px;"></div> 

我new.js.erb:

$('#post-preview').html($('#post_body_html').val()); 

回答

1

對我來說,似乎就像你需要將預覽綁定到能夠工作的東西一樣:

$(document).ready(function() { 
    // Bind it to some action. 
    $('.preview').click(function(){ 
     $('#post-preview').html($('#post_body_html').val()); 
    }); 
}); 

你甚至可以將它綁定到鍵盤按下;就像你在StackOverflow上看到的那樣。

Click事件工作實例:http://jsfiddle.net/kuroir/D7cPT/1/ 自動刷新(上的keyDown)工作實例:http://jsfiddle.net/kuroir/D7cPT/3/

+0

我試圖創造一些這樣的:http://sedition.com/a/2662 – 2011-05-28 18:15:07

+0

您能不能告訴我怎麼樣在網站上做預覽,我鏈接到 – 2011-05-28 19:13:43

+0

@Rails初學者他只是向你展示瞭如何在JavaScript中編寫預覽功能,這正是你想要的。 – 2011-05-29 03:19:39