Ruby 板


LINE

版大提到了Controller 不知道这样对不对,不是script/generate scaffold doctor .... 之後就有样板,没有提供rest功能? 小弟自己有小改一下 装了json的rubygem 之後有成功输出json格式的档案 class DoctorsController < ApplicationController # GET /doctors # GET /doctors.xml def index @doctors = Doctor.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @doctors } format.json { render :json =>@doctors } end end # GET /doctors/1 # GET /doctors/1.xml def show @doctor = Doctor.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @doctor } format.json { render :json=> @doctor } end end # GET /doctors/new # GET /doctors/new.xml def new @doctor = Doctor.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @doctor } format.json { render :json=> @doctor } end end # GET /doctors/1/edit def edit @doctor = Doctor.find(params[:id]) end # POST /doctors # POST /doctors.xml def create @doctor = Doctor.new(params[:doctor]) respond_to do |format| if @doctor.save flash[:notice] = 'Doctor was successfully created.' format.html { redirect_to(@doctor) } format.xml { render :xml => @doctor, :status => :created, :location => @doctor } format.json { render :json=> @doctor, :status => :created, :location => @doctor } else format.html { render :action => "new" } format.xml { render :xml => @doctor.errors, :status => :unprocessable_entity } format.json { render :json => @doctor.errors, :status => :unprocessable_entity } end end end # PUT /doctors/1 # PUT /doctors/1.xml def update @doctor = Doctor.find(params[:id]) respond_to do |format| if @doctor.update_attributes(params[:doctor]) flash[:notice] = 'Doctor was successfully updated.' format.html { redirect_to(@doctor) } format.xml { head :ok } format.json { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @doctor.errors, :status => :unprocessable_entity } format.json { render :json => @doctor.errors, :status => :unprocessable_entity} end end end # DELETE /doctors/1 # DELETE /doctors/1.xml def destroy @doctor = Doctor.find(params[:id]) @doctor.destroy respond_to do |format| format.html { redirect_to(doctors_url) } format.xml { head :ok } format.json { head :ok } end end end ※ 引述《Esh ()》之铭言: : 首先感谢大家之前对小弟之前在REST疑问的排除 : 现在遇到了一个状况 : 就是对规定的URI送出PUT, GET, POST, DELETE : 只有GET跟DELETE的结果正确... : (用了几个REST GUI Client都这样) : 例如有一个doctors的controller : 里面有Uid, Firstname, Lastname, City, 与License五个Fields : 小弟我有修改migrate的内容,把timestamp去掉 : 现在除了上面5个栏位,只有多了会自动产生的ID : 先用Scaffold产生的new样板,网页输入一笔资料後 : 对doctors/1.xml 送出Get Method得到了 : <?xml version="1.0" encoding="UTF-8"?> : <doctor> : <city>CA</city> : <firstname>Peter</firstname> : <id type="integer">1</id> : <lastname>Norton</lastname> : <license type="integer">2147483647</license> : <uid>A128080042</uid> : </doctor> : 接着我想要修改资料,例如把uid改成 A128080142 : 於是再对doctors/1.xml 送出PUT : 以下是封包内容 : <?xml version="1.0" encoding="UTF-8"?> : <doctor> : <city>CA</city> : <firstname>Peter</firstname> : <id type="integer">1</id> : <lastname>Norton</lastname> : <license type="integer">2147483647</license> : <uid>A128080142</uid> : </doctor> : 送出之後,得到200 OK的回应 : 但是再用GET去看这笔资料 : 得到的还是变动前的UID : 另外,要新增一笔资料 : 对doctors.xml送出POST : 一样编辑上述的xml内容 : 一样得到200 ok的回应,也真的多了一笔资料在Database : 可是仅有自动生成的ID栏位多了自动加1的数值 : 该record的其他栏位city, firstname. lastname..... : 都是null的 : 不知道为什麽会发生这样的状况? : 该如何解决? 我送出的方式有误嘛? : 或者是REST Client的问题 (我用python的RESTClient http://restclient.org/: 有请高手解惑了! : 谢谢 -- http://blog.leezhenyu.com Technology, Study, Food, Travel --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.137.254.39
1F:→ godfat:ok, 这样很明显就是 python 丢来的 param 格式跟 rails不同 05/27 17:12
2F:→ godfat:你看看 log 看他实际上是丢什麽过来,然後看能不能改 05/27 17:12







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP