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.

 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: 很不错哦

证明成功了

 

目录
相关文章
|
3月前
|
Ruby
|
3月前
|
Ruby
|
3月前
|
Ruby
|
4月前
|
Ruby
|
4月前
|
Ruby
Ruby 教程 之 Ruby 方法 2
Ruby return 语句
29 0
|
4月前
|
Ruby
Ruby 教程 之 Ruby 方法 3
可变数量的参数
37 1
|
2月前
|
JSON 数据格式 Ruby
|
2月前
|
JSON 数据格式 Ruby
|
2月前
|
存储 JSON 数据格式
|
2月前
|
JSON Ubuntu Linux