- 浏览: 2049088 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
如果你要做rails restful的工作,建议先看看管网的
http://api.rubyonrails.org/classes/ActionController/Resources.html
1. 安裝 Ruby on Rails 基本環境 (windows)
- 下載安裝 1-Click Rails Installer
- 下載安裝 E-TextEditor ,安裝過程會出現錯誤,按下略過即可。啟動時會問你使否安裝 cygwin,請點手動不要安裝。
- 啟動 -> cmd -> cd \
- rails my_event
- cd my_event
- ruby script/server
- 開啟瀏覽器 http://localhost:3000 ,將會看到 Rails 的預設首頁
2. 建立一個 non-RESTful 的 Hello World! Rails Project
- ruby script/generate controller welcome
- 編輯 /app/controllers/welcome_controller.rb,加入
def say render :text => "Hello world!" end
- 瀏覽 http://localhost:3000/welcome/say,將看到 Hello world!
- 編輯 /app/controllers/welcome_controller.rb,加入
def index end
- 新增 /app/views/welcome/index.html.erb,內容是
<p>Hola!</p> <p><%= link_to 'hello world', :controller => 'welcome', :action => 'say' %><p>
- 修改 /config/route.rb 加上 map.root :controller => "welcome"
- 刪除 /public/index.html
- 瀏覽 http://localhost:3000/welcome/say,將看到 Hola! 及 hello 超連結。
3. 實做一個 non-RESTful 的 CRUD
- ruby script/generate model event name:string description:text
- rake db:migrate
- ruby script/generate controller events
- 編輯 /app/controllers/events_controller.rb 加入
def index @events = Event.find(:all) end def show @event = Event.find(params[:id]) end def new @event = Event.new end def create @event = Event.new(params[:event]) @event.save redirect_to :action => :index end def edit @event = Event.find(params[:id]) end def update @event = Event.find(params[:id]) @event.update_attributes(params[:event]) redirect_to :action => :show, :id => @event end def destroy @event = Event.find(params[:id]) @event.destroy redirect_to :action => :index end
- 新增 /app/views/events/index.html.erb,內容如下:
<ul> <% @events.each do |event| %> <li> <%= link_to event.name, :controller => 'events', :action => 'show', :id => event %> <%= link_to 'edit', :controller => 'events', :action => 'edit', :id => event %> <%= link_to 'delete', :controller => 'events', :action => 'destroy', :id => event %> </li> <% end -%> </ul> <%= link_to 'new event', :controller => 'events', :action => 'new' %>
- 新增 /app/views/events/show.html.erb,內容如下:
<%=h @event.name %> <%=h @event.description %> <p><%= link_to 'back to index', :controller => 'events', :action => 'index' %></p>
- 新增 /app/views/events/new.html.erb,內容如下:
<% form_for @event, :url => { :controller => 'events', :action => 'create' } do |f| %> <%= f.label :name, "Name" %> <%= f.text_field :name %> <%= f.label :description, "Description" %> <%= f.text_area :description %> <%= f.submit "Create" %> <% end %>
- 新增 /app/views/events/edit.html.erb,內容如下:
<% form_for @event, :url => { :controller => 'events', :action => 'update', :id => @event } do |f| %> <%= f.label :name, "Name" %> <%= f.text_field :name %> <%= f.label :description, "Description" %> <%= f.text_area :description %> <%= f.submit "Update" %> <% end %>
- 連往 http://localhost:3000/events
4. 修改成一個 RESTful 版本的 CRUD
map.resources :events
-
<% @events.each do |event| %>
<li>
<%= link_to event.name, event_path(event) %>
<%= link_to 'edit', edit_event_path(event) %>
<%= link_to 'delete', event_path(event), :method => :delete %>
<% end -%>
</ul>
<%= link_to 'new event', new_event_path %>
<%=h @event.name %> <%=h @event.description %> <p><%= link_to 'back to index', events_path %>
<% form_for @event, :url => events_path do |f| %> <%= f.label :name, "Name" %> <%= f.text_field :name %> <%= f.label :description, "Description" %> <%= f.text_area :description %> <%= f.submit "Create" %> <% end %>
<% form_for @event, :url => event_path(@event), :html => { :method => :put } do |f| %> <%= f.label :name, "Name" %> <%= f.text_field :name %> <%= f.label :description, "Description" %> <%= f.text_area :description %> <%= f.submit "Update" %> <% end %>
5. 使用 RESTful 版的 Scaffold
- ruby script/generate scaffold person name:string bio:text birthday:datetime
- rake db:migrate
- Ctrl+C 關閉 Server,重新啟動 script/server
- 瀏覽 http://localhost:3000/people 並操作 CRUD
6. Ajax 實做練習1 (傳回HTML更新)
- 新增 /app/views/layout/application.html.erb,內容如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <%= javascript_include_tag :defaults %> </head> <body> <%= yield %> </body> </html>
- 編輯 /app/views/welcome/index.html.erb,加入
<p><%= link_to_remote 'Ajax hello', :url => { :controller => 'welcome', :action => 'say' }, :update => 'foobar' %></p> <div id="foobar"> </div>
- 瀏覽http://localhost:3000
7. Ajax 實做練習2 (使用RJS)
- 編輯 /app/views/events/index.html.erb,在迴圈中間和文件最後加入
<%= link_to_remote 'ajax show', :url => event_path(event), :method => :get %>
<div id="content"> </div>
- 編輯 /app/controllers/events_controller.rb,在 show action 中加入
respond_to do |format| format.html format.js end
- 新增 /app/views/events/_event.html.erb,內容與 show.html.erb 相同
- 新增 /app/views/events/show.js.rjs,內容如下
page.replace_html 'content', :partial =>'event' page.visual_effect :highlight, 'content'
- 瀏覽http://localhost:3000/events
8. 使用者註冊登入 Generator 產生器
- 下載 http://github.com/technoweenie/restful-authentication 到 /vendor/plugins/,命名為 restful-authentication
- 執行 ruby script/generate authenticated user sessions --old-passwords
- rake db:migrate
- Ctrl+C 關閉 Server,重新啟動 script/server
- 連往 http://localhost:3000/users/new 註冊頁面
- 編輯 /app/controllers/application.rb,加入
include AuthenticatedSystem
- 編輯 /app/views/layout/application.html.erb,加入
<%= render :partial => "users/user_bar" %>
- 編輯 /app/controllers/events_controller.rb,加入
before_filter :login_required
- 點選 Log out,再連往 http://localhost:3000/events/ 將導到登入頁面
9. 分頁 Plugin
- 下載 http://github.com/mislav/will_paginate 到 /vendor/plugins/,命名為 will_paginate
- 修改 /app/controllers/events_controller.rb 的 index action 如下
def index @events = Event.paginate(:page => params[:page], :per_page => 3, :order => "id DESC") end
- 編輯 /app/views/events/index.html.erb,加入
<%= will_paginate @events %>
- 連往 http://localhost:3000/events/
发表评论
-
Destroying a Postgres DB on Heroku
2013-04-24 10:58 890heroku pg:reset DATABASE -
VIM ctags setup ack
2012-04-17 22:13 3215reference ctags --extra=+f --e ... -
alias_method_chain方法在3.1以后的替代使用方式
2012-02-04 02:14 3240alias_method_chain() 是rails里的一个 ... -
一些快速解决的问题
2012-01-19 12:35 1435问题如下: 引用Could not open library ... -
API service 安全问题
2011-12-04 08:47 1342这是一个长期关注的课题 rest api Service的 ... -
Module方法调用好不好
2011-11-20 01:58 1300以前说,用module给class加singleton方法,和 ... -
一个ajax和rails交互的例子
2011-11-19 01:53 1859首先,这里用了一个,query信息解析的包,如下 https: ... -
Rails 返回hash给javascript
2011-11-19 01:43 2238这是一个特别的,不太正统的需求, 因为,大部分时候,ajax的 ... -
关于Rubymine
2011-11-18 23:21 2229开个帖子收集有关使用上的问题 前一段时间,看到半价就买了。想 ... -
ruby中和javascript中,动态方法的创建
2011-11-18 21:01 1192class Klass def hello(*args) ... -
textmate快捷键 汇总
2011-11-16 07:20 8091TextMate 列编辑模式 按住 Alt 键,用鼠标选择要 ... -
Ruby面试系列六,面试继续面试
2011-11-15 05:55 1980刚才受到打击了,充分报漏了自己基础不扎实,不肯向虎炮等兄弟学习 ... -
说说sharding
2011-11-13 00:53 1434这个东西一面试就有人 ... -
rails面试碎碎念
2011-11-12 23:51 1898面试继续面试 又有问ru ... -
最通常的git push reject 和non-fast forward是因为
2011-11-12 23:29 17147git push To git@github.com:use ... -
Rails 自身的many to many关系 self has_many
2011-11-12 01:43 2678简单点的 #注意外键在person上people: id ... -
Rails 3下的 in place editor edit in place
2011-11-12 01:20 913第一个版本 http://code.google.com/p ... -
Heroku 的诡异问题集合
2011-11-11 07:22 1657开个Post记录,在用heroku过程中的一些诡异问题和要注意 ... -
SCSS 和 SASS 和 HAML 和CoffeeScript
2011-11-07 07:52 12901Asset Pipeline 提供了内建 ... -
Invalid gemspec because of the date format in specification
2011-11-07 02:14 2072又是这个date format的错误。 上次出错忘了,记录下 ...
相关推荐
restful例子
一个简单的例子,有助于开发人员参考进行后续开发,一个简单的demo
restful2个例子
restful架构, c#案例,源代码,文档中可直接看详解。。
Spring restful开发小例子,供初学者使用
restful接口示例代码restful接口示例代码restful接口示例代码restful接口示例代码
restful web,restful web,restful web
CXF Restful服务简单例子
spring boot restful服务小例子,从spring boot官网下载下来,仅用来自己学习。
restful结合spring实例,带有两个例子。一个是mvc的
springBoot demoe 包含 单jar启动例子 restful例子 和jar转war例子
实例主要做移动开发API实用,基于RESTful ,后期实用angularJS 解决了跨域问题,解决了PUT、DELETE的405报错问题,, 2天的时间啊,,被网上各种方法方式各种扯、拽 ,搞得乱了脑子了,静下来写写 ok了
RestFul API 案例 新手入门帮助 包括RestFul 和RestFul Api的介绍 也包括案例代码
开发restful接口应该遵循统一的规范,保持规范的统一才能方便维护和应用
Restful 简单例子
spring boot3+jpa+lombok+mapstruct实现的restful api例子
restful WebService的入门例子.pdfrestful WebService的入门例子.pdf
CXF发布restful WebService的入门例子.pdfCXF发布restful WebService的入门例子.pdf
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs. FEATURES Powerful router ...Route parameters with wildcards and conditions Route redirect, halt, ...
Spring Hibernate, Jersey 创建restful 服务的例子 图文并茂,step by step