`

Restful例子

阅读更多

如果你要做rails restful的工作,建议先看看管网的

http://api.rubyonrails.org/classes/ActionController/Resources.html



1. 安裝 Ruby on Rails 基本環境 (windows)

  1. 下載安裝 1-Click Rails Installer
  2. 下載安裝 E-TextEditor ,安裝過程會出現錯誤,按下略過即可。啟動時會問你使否安裝 cygwin,請點手動不要安裝。
  3. 啟動 -> cmd -> cd \
  4. rails my_event
  5. cd my_event
  6. ruby script/server
  7. 開啟瀏覽器 http://localhost:3000 ,將會看到 Rails 的預設首頁

2. 建立一個 non-RESTful 的 Hello World! Rails Project

  1. ruby script/generate controller welcome
  2. 編輯 /app/controllers/welcome_controller.rb,加入
    		def say
    		 render :text => "Hello world!"
    		end
    	
  3. 瀏覽 http://localhost:3000/welcome/say,將看到 Hello world!
  4. 編輯 /app/controllers/welcome_controller.rb,加入
    		def index
    		end
    	
  5. 新增 /app/views/welcome/index.html.erb,內容是
    		<p>Hola!</p>
    		<p><%= link_to 'hello world', :controller => 'welcome', :action => 'say' %><p>
    	
  6. 修改 /config/route.rb 加上 map.root :controller => "welcome"
  7. 刪除 /public/index.html
  8. 瀏覽 http://localhost:3000/welcome/say,將看到 Hola! 及 hello 超連結。

3. 實做一個 non-RESTful 的 CRUD

  1. ruby script/generate model event name:string description:text
  2. rake db:migrate
  3. ruby script/generate controller events
  4. 編輯 /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
    		
  5. 新增 /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' %>
    	
  6. 新增 /app/views/events/show.html.erb,內容如下:
    <%=h @event.name %>
    <%=h @event.description %>
    
    <p><%= link_to 'back to index', :controller => 'events', :action => 'index' %></p>
    		
  7. 新增 /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 %>
    		
  8. 新增 /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 %>
    		
  9. 連往 http://localhost:3000/events

4. 修改成一個 RESTful 版本的 CRUD

  • 編輯 /config/routes.rb,加入
    map.resources :events
    	
  • 編輯 /app/views/events/index.html.erb,內容如下:
    
    
      <% @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 %>
  • 編輯 /app/views/events/show.html.erb,內容如下:
    <%=h @event.name %>
    <%=h @event.description %>
    
    <p><%= link_to 'back to index', events_path %>

     

  • 編輯 /app/views/events/new.html.erb,內容如下:
    <% 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 %>
    
  • 編輯 /app/views/events/edit.html.erb,內容如下:
    <% 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

    1. ruby script/generate scaffold person name:string bio:text birthday:datetime
    2. rake db:migrate
    3. Ctrl+C 關閉 Server,重新啟動 script/server
    4. 瀏覽 http://localhost:3000/people 並操作 CRUD

    6. Ajax 實做練習1 (傳回HTML更新)

    1. 新增 /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>
      	
    2. 編輯 /app/views/welcome/index.html.erb,加入
      	  
      <p><%= link_to_remote 'Ajax hello', :url => { :controller => 'welcome', :action => 'say' }, :update => 'foobar' %></p>
      
      
      <div id="foobar">
      </div>
      
    3. 瀏覽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/
    分享到:
    评论

    相关推荐

      一个简单的Restful例子以及讲解

      ### 一个简单的RESTful例子及深入讲解 #### REST简介与核心概念 REST(Representational State Transfer,表象性状态转移)是一种软件架构风格,尤其适用于Web服务的设计。它利用现有的互联网标准和技术,如HTTP、...

      restful2个例子

      在本文中,我们将深入探讨RESTful架构风格的两个实际应用示例,它们分别是基于Apache CXF实现的`cxf_demo`和基于Spring框架的`Spring_Restful_Demo`。 首先,让我们了解RESTful的基本原则: 1. **资源(Resources...

      restful例子

      这个例子是关于如何在Spring 3.0框架中实现RESTful服务的,Spring是Java开发中最常用的一个开源框架,特别适合构建企业级的Web应用。 首先,理解RESTful的核心概念: 1. 资源(Resources):在Web服务中,资源是...

      springRestful小例子

      本文将深入探讨Spring框架如何实现RESTful API,以及这些概念在"springRestful小例子"中的具体应用。 首先,我们要理解什么是RESTful。REST(Representational State Transfer)是一种网络应用程序的设计风格和开发...

      restful web例子

      restful web,restful web,restful web

      restful接口示例代码

      在这个例子中,我们定义了一个名为`UserController`的类,它处理与用户相关的REST请求。每个方法都对应一个HTTP动词和一个URL路径。`@RestController`注解表明这是一个处理HTTP请求的控制器,`@RequestMapping("/...

      restful结合spring实例,带有两个例子

      在IT行业中,RESTful是一种广泛...通过这两个例子,开发者可以深入理解RESTful设计原则,熟悉Spring MVC的使用,以及掌握构建高效、健壮的Web服务的技巧。在实际开发中,这样的技能对于构建现代、可扩展的API至关重要。

      CXF Restful服务简单例子

      【CXF Restful服务简单例子】\n\n在IT行业中,Apache CXF是一个广泛使用的开源框架,它允许开发人员创建和消费Web服务。RESTful(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于...

      springBoot demoe

      接下来是"restful例子"。REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,用于分布式系统中的数据交互。SpringBoot提供了强大的支持来构建RESTful API。你可以使用`...

      spring boot restful服务小例子

      在“spring boot restful服务小例子”中,我们重点讨论的是如何使用 Spring Boot 构建 RESTful Web 服务。REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于 HTTP 协议,可以...

      ASP.net RESTful最实用个例子,实例化用linqtosql,用作angularJS后台

      实例主要做移动开发API实用,基于RESTful ,后期实用angularJS 解决了跨域问题,解决了PUT、DELETE的405报错问题,, 2天的时间啊,,被网上各种方法方式各种扯、拽 ,搞得乱了脑子了,静下来写写 ok了

      restful 接口开发规范(RESTfulAPIdesignguide)

      在开发RESTful接口时,我们需要遵循一定的设计规范来确保接口的一致性、可维护性和易用性。RESTful API(Representational State Transfer,也称为RESTful web服务)是一种提供互联网计算机系统间互操作性的方法。...

      restful 请求和返回XML的例子

      RESTful是一种Web服务设计...通过这个例子,我们可以学习如何在实际开发中使用RESTful API和XML进行数据交换,以及如何利用CXF这样的框架简化这一过程。理解这些概念和技术对于构建可扩展、可维护的Web服务至关重要。

      RestFul API 案例

      **RestFul API 知识点详解** REST(Representational State Transfer)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,以数据为中心,强调资源的状态转移。RESTful API是遵循REST原则设计的Web服务接口,...

      RESTful WebService 例子

      在这个例子中,我们将探讨如何开发一个使用JSON(JavaScript Object Notation)作为数据交换格式的RESTful Web Service。JSON是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它是Web服务...

      restful风格请求,token鉴权实例

      在IT行业中,RESTful风格的API设计已经成为现代Web服务的标准,它强调资源的表述状态转移。本实例将探讨如何在Spring Boot框架下实现RESTful风格的请求,并利用JWT(JSON Web Token)进行权限验证,同时结合自定义...

      浅谈java调用Restful API接口的方式

      "Java调用Restful API接口的方式" Java调用Restful API接口是Java开发中非常重要的一部分,了解Java调用Restful API接口的方式可以帮助开发者更好地理解和使用相关技术。本文将详细介绍Java调用Restful API接口的...

      PHP restful service 例子

      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, ...

      restful接口实现Demo

      在这个例子中,客户端向服务器发送GET请求,读取响应内容并打印出来。 在解压的文件列表"RESTfulWS"中,可能包含了实现上述RESTful接口和服务端的配置文件,以及可能的测试用例或样例数据。这些内容可以帮助开发者...

      CXF发布restful WebService的入门例子.pdf

      在本入门例子中,我们将使用CXF来发布一个RESTful WebService,以管理房间和居住在房间内的人物。 1. **POJO对象定义**: 在RESTful WebService中,数据通常以Java对象(POJO)的形式进行处理。这里的`Person`和`...

    Global site tag (gtag.js) - Google Analytics