1. rails 2.0以上版本下载actionwebservice插件。
2. 放到项目的vendor/plugins目录下
3. 配置config/environment.rb:
a.在 Rails::Initializer.run do |config|...end之前加入:
class Rails::Configuration
attr_accessor :action_web_service
end
b.在 Rails::Initializer.run do |config|...end之间加入:
config.frameworks += [ :action_web_service]
config.action_web_service = Rails::OrderedOptions.new
config.load_paths += %W( #{RAILS_ROOT}/app/apis
#{RAILS_ROOT}/vendor/plugins/actionwebservice/lib)
c.在 Rails::Initializer.run do |config|...end之后加入:
require 'action_web_service'
4.app下新建目录:apis
5.在app/apis下新建文件:person_api.rb:
class PersonApi < ActionWebService::API::Base
inflect_names false
api_method :add, :expects => [:string], :returns => [:int]
api_method :remove, :expects => [:int], :returns => [:bool]
end
6.在controller里新建person_controller.rb:
class PersonController < ActionController::Base
protect_from_forgery .....# 如果application.rb里有就需要加上
layout "invoke"
web_service_api PersonApi
web_service_scaffold :invoke
def add
return 1
end
def remove
end
end
7. views下新建目录:person/invoke
8.将\vendor\plugins\actionwebservice\lib\action_web_service\templates\scaffolds下的methods.erb,parameters.erb, result.erb放到新建的目录下。
9.将layout.erb改名为invoke.erb放到views/layouts下。
10.vendor\plugins\actionwebservice\lib\action_web_service\scaffolding.rb的render_invocation_scaffold方法:
if self.active_layout.nil?
render :file => scaffold_path("layout")
else
render :file => self.active_layout
end
改为:
if self.active_layout.nil?
render :file => scaffold_path("layout")
else
render :template => self.active_layout
end
8. 重启服务,在浏览器中输入:http://localhost:3000/person/invoke,出现方法列表,ok!
ActionWebService is back
ActionWebService - how to raise SOAP::FaultError