每天一剂Rails良药之Adding Simple Web Serices To Your Actions

1,对于接收WebService,我们完全不用管,因为默认时任何ContentType为"application/xml"的POST都将被Rails内建的XmlSimple解析并转换为参数的Hash,这样我们就始终可以在controller里使用params方法得到参数
2,对于返回WebService,可以这样做
app/controllers/contacts_controller.rb
def create
  @contact = Contact.create(params[:contact])
  respond_to do |format|
    format.html do
      flash[:notice] = 'Contact was successfully created.'
      redirect_to :action => 'list'
    end
    format.xml { render(:xml => @contact.to_xml, :status => "201 Created")}
    format.rss
    format.js
  end
end

其实RESTful开发时 ruby script/generate scaffold_resource自动生成的controller里就有 respond_to do |format| ...的代码

你可能感兴趣的:(Web,xml,webservice,Ruby,Rails)