首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails API ActionController::RoutingError:没有路由匹配[POST] "/goals“

Rails API ActionController::RoutingError:没有路由匹配[POST] "/goals“
EN

Stack Overflow用户
提问于 2014-04-17 01:52:16
回答 1查看 1.5K关注 0票数 2

我正在用Rails创建一个简单的API,而我创建目标资源的测试由于一个RoutingError而失败--但我不知道为什么。

当我执行rake routes时,我可以看到POST /goals路由是存在的。在我的routes.rb中,我已经将namespace :api设置为path: '/',所以我认为我的帖子请求应该是有效的。

有人能帮我指出我做错了什么吗?

这是我的routes.rb

代码语言:javascript
复制
Rails.application.routes.draw do

  with_options except: [:new, :edit] do |list_only|
    namespace :api, path: '/', constraints: { subdomain: 'api' }  do 
      list_only.resources :goal           
    end
  end
end

以下是我的路线:

代码语言:javascript
复制
$ rake routes

   Prefix Verb   URI Pattern          Controller#Action
api_goals GET    /goals(.:format)     api/goals#index {:subdomain=>"api"}
          POST   /goals(.:format)     api/goals#create {:subdomain=>"api"}
 api_goal GET    /goals/:id(.:format) api/goals#show {:subdomain=>"api"}
          PATCH  /goals/:id(.:format) api/goals#update {:subdomain=>"api"}
          PUT    /goals/:id(.:format) api/goals#update {:subdomain=>"api"}
          DELETE /goals/:id(.:format) api/goals#destroy {:subdomain=>"api"}

我的goals_controller.rb

代码语言:javascript
复制
  class API::GoalsController < ApplicationController
      before_action :set_goal, only: [:show, :edit, :update, :destroy]

      def index
        goals = Goal.all
        if is_complete = params[:is_complete]
          goals = goals.where(is_complete: is_complete)
        end
        render json: goals, status: 200             
      end

      def show
        goal = Goal.find(params[:id])
        render json: goal, status: 200
      end

      def create
        goal = Goal.new(goal_params)
        if goal.save
          # render nothing: true, status: 204, location: goal
          head 204, location: goal
        end
      end

      private
        # Use callbacks to share common setup or constraints between actions.
        def set_goal
          @goal = Goal.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def goal_params
          params.require(:goal).permit(:description, :motivation, :completion_date, :is_complete)
        end
    end

下面是一个测试:

代码语言:javascript
复制
require 'test_helper'

    class CreatingGoalsTest < ActionDispatch::IntegrationTest
      test 'creates goals' do 
        post '/goals', 
          { 
            description: "string", 
            motivation: "another string",
            completion_date: "01/01/2014",
            is_complete: true
          }.to_json,
          { 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s }

        assert_equal 201, response.status
        assert_equal Mime::JSON, response.content_type

        goal = json(response.body)
        assert_equal goal_url(goal[:id]), response.location 
      end
    end

以下是错误:

代码语言:javascript
复制
$ rake test:integration
Run options: --seed 27226

# Running:

E....

Finished in 0.102621s, 48.7230 runs/s, 116.9351 assertions/s.

  1) Error:
CreatingGoalsTest#test_creates_goals:
ActionController::RoutingError: No route matches [POST] "/goals"
    test/integration/creating_goals_test.rb:5:in `block in <class:CreatingGoalsTest>'

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-17 03:26:03

如果没有在测试中设置子域(正如rails所期望的那样),则可以通过添加以下内容来覆盖所提供的主机:

代码语言:javascript
复制
def setup
  host! 'api.example.com'
end

去你的测试套件。

代码语言:javascript
复制
class CreatingGoalsTest < ActionDispatch::IntegrationTest
  def setup
    host! 'api.example.com'
  end

  test 'creates goals' do 
    post '/goals', 
      { 
        description: "string", 
        motivation: "another string",
        completion_date: "01/01/2014",
        is_complete: true
      }.to_json,
      { 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s }

    assert_equal 201, response.status
    assert_equal Mime::JSON, response.content_type

    goal = json(response.body)
    assert_equal goal_url(goal[:id]), response.location 
  end
end

关于rails中的子域和测试的进一步资源:带有子域的Rails测试

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23123226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档