与 API 样式控制器中的操作交互
我正在制作一个 Ruby on Rails 应用程序,其中有:
routes.rb
:
Test::Application.routes.draw do
get "api_error/404"
get "sss/home"
root :to => "home#index"
match "/zad0xsis" => "home#pablo"
match "/sss" => "sss#home"
match "/api/1/:api_action" => "api_v1#runner"
match "/api/" => "api_error#api_not_found"
match "/api/:api_version/:api_action" => "api_error#api_not_found"
match "/api/:api_version" => "api_error#api_not_found"
match "/api/1/:api_action/:whoiscool" => "api_v1#runner"
match "/whoscool/:whoiscool" => "api_v1#whoscool"
end
api_v1_controller.rb
:
class ApiV1Controller < ApplicationController
def runner
response.headers["Content-Type"] = 'application/json'
response.headers["Access-Control-Allow-Origin"] = '*'
response.headers["server"] = `hostname`
@output = {'api_version' => "1", "error"=>false}
case params[:api_action]
when "register"
register
when "whoscool"
whoscool
else
@output['error'] = true
@output['error_code'] = 106
@output['human_error'] = "API Function not found (or not authorized)"
end
# Set Output
@output = JSON.generate(@output)
# Turn Off Layout
render :layout => false
end
#--------------------------
# Register Action
#--------------------------
def register
@output['hello'] = "true"
end
def whoscool
@output['cool is'] = params[:whoiscool]
end
#----- ADD NEW FUNCTIONS ABOVE THIS LINE -------#
end
whoscool_controller.rb
:
class WhoscoolController < ApplicationController
def index
end
end
我需要知道如何让 whoscool_controller.rb
调用 api_v1_controller.rb
runner
使其调用操作whoscool
。如果我访问 api/1/whoscool/zad0xsis
,我会得到正确的 JSON 输出,但在访问 whoscool/zad0xsis
时我需要获取该输出。谢谢!
I'm making an Ruby on Rails app where I have:
routes.rb
:
Test::Application.routes.draw do
get "api_error/404"
get "sss/home"
root :to => "home#index"
match "/zad0xsis" => "home#pablo"
match "/sss" => "sss#home"
match "/api/1/:api_action" => "api_v1#runner"
match "/api/" => "api_error#api_not_found"
match "/api/:api_version/:api_action" => "api_error#api_not_found"
match "/api/:api_version" => "api_error#api_not_found"
match "/api/1/:api_action/:whoiscool" => "api_v1#runner"
match "/whoscool/:whoiscool" => "api_v1#whoscool"
end
api_v1_controller.rb
:
class ApiV1Controller < ApplicationController
def runner
response.headers["Content-Type"] = 'application/json'
response.headers["Access-Control-Allow-Origin"] = '*'
response.headers["server"] = `hostname`
@output = {'api_version' => "1", "error"=>false}
case params[:api_action]
when "register"
register
when "whoscool"
whoscool
else
@output['error'] = true
@output['error_code'] = 106
@output['human_error'] = "API Function not found (or not authorized)"
end
# Set Output
@output = JSON.generate(@output)
# Turn Off Layout
render :layout => false
end
#--------------------------
# Register Action
#--------------------------
def register
@output['hello'] = "true"
end
def whoscool
@output['cool is'] = params[:whoiscool]
end
#----- ADD NEW FUNCTIONS ABOVE THIS LINE -------#
end
whoscool_controller.rb
:
class WhoscoolController < ApplicationController
def index
end
end
I need to know how to make whoscool_controller.rb
call api_v1_controller.rb
runner
for making it call the action whoscool
. If I access to api/1/whoscool/zad0xsis
I get the correct JSON output, but I'd need to get that output when accessing whoscool/zad0xsis
instead. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没听错的话,您想要做的是让两个不同的路径调用相同的功能但具有不同的格式,只需为此添加几个路由,如下所示:
然后使
WhoscoolController
句柄不同的格式:If I hear you correctly, what you want to do is have two different paths call the same functionality but with different formats, just add a couple of routes for that, like this:
And then make the
WhoscoolController
handle the different formats: