每天一剂Rails良药之Live Preview

news/2024/9/8 16:21:08 标签: Rails, prototype, ActiveRecord, JavaScript
像蛙眼等有时候需要给用户提供预览查看,今天我们就来看看利用Rails和Prototype快捷的提供Live Preview功能。

1,在layout里加入prototype.js
如app/views/layouts/standard.rhtml:
[code]
<html>
<head>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<%= yield %>
</body>
</html>
[/code]

2,定义Entry类
如app/models/entry.rb:
[code]
class Entry
attr_accessor :title, :body
end
[/code]
我们的demo中的模型没有继承ActiveRecord::Base类,只是简单的提供两个可访问的字段

3,定义controller
如app/controllers/diary_controller.rb:
[code]
class DiaryController < ApplicationController
layout 'standard'

def new
@entry = Entry.new
end

def preview
render :layout => false
end
end
[/code]

4,定义rhtml模板
app/views/diary/new.rhtml:
[code]
<%= start_form_tag({:action => "save"}, :id => "entry-form")%>
Title:<%= text_field :entry, :title %><br/>
Content:<%= text_field :entry, :body %><br/>
<%= submit_tag "Save" %>
<%= end_form_tag %>

<%= 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>
[/code]
app/views/diary/preview.rhtml:
[code]
<h2>Diary entry preview</h2>
<h3><%= params[:entry][:title]%></h3>
<%= textilize params[:entry][:body] %>
[/code]

5,打完收工
访问[url]http://localhost:3000/diary/new[/url]即可看到Live Preview效果

http://www.niftyadmin.cn/n/729003.html

相关文章

python分布式计算框架_Ray 分布式计算框架介绍

Ray 介绍 Ray 是 UC Berkeley RISELab 出品的机器学习分布式框架。UC Berkeley 教授 Ion Stoica 写了一篇文章&#xff1a;The Future of Computing is Distributed。里面详细说了 Ray 产生的原由。总结一下&#xff0c;就是由于 AI 和大数据的快速发展&#xff0c;对于应用和硬…

每天一剂Rails良药之Live Search

这次我们看看Rails里text_field的auto_complete&#xff0c;即Live Search。1&#xff0c;添加Recipe的Migration [code] class AddRecipes < ActiveRecord::Migrationdef self.upcreate_table :recipes do |t|t.column :name, :stringendenddef self.downdrop_table :recip…

MySQL的索引有什么,什么情况下不会用到索引

MySQL的索引有什么&#xff0c;什么情况下不会用到索引1. MySQL中索引有哪些类型1.1 普通索引1.2 主键索引(也叫唯一索引)1.3 复合索引1.4 全文索引1.5 空间索引2. 什么情况下不会用到索引3. 什么情况下不推荐使用索引&#xff1f;4. 联合索引的优势4.1 减少开销4.2 覆盖索引4.…

python 回车字符_一起学Python | 先导篇——第一个Python程序

先导篇 0.1 第一个Python程序0.0.1 Python程序安装1. 在Windows上安装Python从Python官网https://www.python.org/downloads/下载Python 3.8的安装程序&#xff0c;下载后双击运行&#xff0c;如图0.0.1。特别要注意勾上Add Python 3.8 to PATH&#xff0c;然后点击“Customize…

每天一剂Rails良药之Cheap and Easy Theme Support

你可能需要给你的站点用户添加一个主题样式支持。 让我们看看用Rails实现该功能是多简单。1&#xff0c;给User模型添加类型为string的style字段2&#xff0c;修改app/views/layouts/application.rhtml [code] <html><head><% stylesheet_link_tag(session[:use…

python获取json并解析_Python 3获取和解析JSON API

How would I parse a json api response with python? I currently have this: import urllib.request import json url https://hacker-news.firebaseio.com/v0/topstories.json?printpretty def response(url): with urllib.request.urlopen(url) as response: return res…

每天一剂Rails良药之Use Ajax to Trim Fast, Static Pages

今天让我们看看怎样使用Rails和Ajax得到静态页面。 看下面这个页面: [code] <ul><li><div id"product-1" class"product-overview"><span class"title">Learn to Program (Chris Pine)<% link_to_remote "detai…

Spring 管理事务的⽅式有⼏种?

Spring 管理事务的⽅式有几种&#xff1f;1. 编程式事务&#xff0c;在代码中硬编码 (不推荐使用)2. 声明式事务&#xff0c;在配置⽂件中配置&#xff08;推荐使用&#xff09;2.1 基于XML的声明式事务2.2 基于注解的声明式事务1. 编程式事务&#xff0c;在代码中硬编码 (不推…