如何在Rails 5 API模式下加入 "新 "路由[找不到 "show "这个动作]。

问题描述 投票:0回答:1

目前,我得到这个错误

The action 'show' could not be found for Api::V1::ImagesController

我的路由文件是这样的

namespace :api do
    namespace :v1 do
        ...
        resources :images
        ...
    end
end

我还可以看到,资源方法没有按照以下要求创建新的端点 文献

我已经暂时这样做了,但我很想知道是否有一个更简单的修复方法,目前我得到了这样的错误:The action 'show' could not found for Api::V1::ImagesController 我的路由文件看起来像这样:namespace :api do namespace :v1 do ... ..:

class Api::V1::ImagesController < Api::BaseController

  def show
    new if params[:id] == "new"
  end

  def new
    ...
ruby-on-rails routes actioncontroller
1个回答
1
投票
namespace :api do
  namespace :v1 do
    resources :images do
      get :new
    end
  end
end

或者,如果你想对多个资源进行这样的操作,可以使用路由关注。

concern :has_new do
  get :new
end

namespace :api do
  namespace :v1 do
    resources :images, concerns: :has_new
    resources :videos, concerns: :has_new
  end
end

我不认为实际上有一个选项可以重新添加到 newedit 的路线。resources 当你的应用程序只在api模式下运行时,你就需要调用。而且你还必须在你的应用程序在仅有api模式下运行的时候,使用猴子补丁 ActionDispatch::Routing::Mapper::资源 来改变行为,如果你想对所有的路由都这样做的话。


-1
投票

你想拥有一个显示路由吗? 如果你想,那么你需要添加show方法。 你不需要在里面添加任何东西。

def show; end

如果你不想包含show route而仍然使用资源助手,你可以使用 only 在您的资源中。 resources :images, only: [:new]

© www.soinside.com 2019 - 2024. All rights reserved.