Live Preview [Rails Recipes]

用prototype完成用户预览功能。

在layout里面加载prototype.js

<%= include_javascript_tag "prototype" %>


用一个日记的例子,定义一个model class Entry, 在里面定义title, body.

class Entry
attr_accessor :title, :body
end


还有一个DiaryController:

def new
@entry = Entry.new
end


定义这个action的view

<% form_tag(:action => "save",:id => "entry-form") do %>
<%= text_field :entry, :title %><br />
<%= text_area :entry, :body %><br />
<%= submit_tag "Save" %>
<% end %>
<%= observe_form "entry-form",
:frequency => 1,
:update => "live-preview",
:complete => "Element.show(' live-preview' )",
:url => { :action => "preview" } %>
<div id="live-preview" style="display: none; border: 1px solid"></div>


完成preview action

def preview
render :layout => false
end


The layout

<h2>Diary entry preview</h2>
<h3><%= params[:entry][:title] %></h3>
<%= textilize params[:entry][:body] %>


textilize need RedCloth

要在contoller里面处理一下回车和<br>才能正常显示 :)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值