与 API 样式控制器中的操作交互

发布于 2024-12-03 19:53:16 字数 2106 浏览 2 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-12-10 19:53:16

如果我没听错的话,您想要做的是让两个不同的路径调用相同的功能但具有不同的格式,只需为此添加几个路由,如下所示:

match 'whoscool/:id' => 'whoscool#show'

scope 'api/v1' do
  match 'whoscool/:id' => 'whoscool#show', :format => 'json'
end

然后使 WhoscoolController 句柄不同的格式:

class WhoscoolController < ApplicationController
  def show
    respond_to do |format|
      format.html { ... }
      format.json { ... }
    end
  end
end

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:

match 'whoscool/:id' => 'whoscool#show'

scope 'api/v1' do
  match 'whoscool/:id' => 'whoscool#show', :format => 'json'
end

And then make the WhoscoolController handle the different formats:

class WhoscoolController < ApplicationController
  def show
    respond_to do |format|
      format.html { ... }
      format.json { ... }
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文