zoukankan      html  css  js  c++  java
  • Ruby on Rail 实现 REST And ActiveResource

     rails new blog //创建应用
     cd blog //进入应用目录
     rails generate scaffold Post name:stringtitle:stringcontent:text

    rake db:migrate //创建数据表

     rails server -p 888888


    4. cd .. 
    5 rails new notebook 
    6 cd notebook
    7 rails generate scaffold book name:string

    rake db:migrate //创建数据表

    在Mode目录 下创建一个 Post.rb

    class Post < ActiveResource::Base
    self.site = 'http://localhost:888888'
    end

    这样在controller里,可以调用post的方法了,比如Post.new或者Post.all

    创建 welcomeController

    $ rails generate controller welcome index

    有些问题查看前一篇文章

    打开 welcomecontroller.rb编辑

    class WelcomeController < ApplicationController
    def index
    # notes = Note.find :all
    notes = Note.all
    puts "I see #{notes.size} note(s):"
    notes.each do |note|
    puts " #{note.date}: #{note.body}"
    end
    end
    end

    读者可以任意修改

    rails server 

    http://localhost:3000/

    终端下出现了

    [2013-01-23 21:23:10] INFO WEBrick::HTTPServer#start: pid=4683 port=3000
    I see 2 note(s):
    2013-01-23: 你好,世界
    2013-01-23: 很不错哦

    证明成功了

  • 相关阅读:
    OGNL与值栈
    Struts2的数据封装
    Struts2页面配置和访问servlet API
    Struts2入门介绍(二)
    Struts2 入门介绍(一)
    Hibernate批量抓取
    Problem G: STL——整理唱片(list的使用)
    STL详细介绍(更新中~~~)
    Problem E: 数量的类模板
    CF: Long Number
  • 原文地址:https://www.cnblogs.com/jackluo/p/2872997.html
Copyright © 2011-2022 走看看