레일스로 간단한 API를 만들고 목표 리소스를 만드는 테스트가 RoutingError
때문에 실패합니다. 왜.Rails API ActionController :: RoutingError : [POST]와 일치하는 경로가 없습니다. "/ goals"
내가 rake routes
일 때 POST /goals
경로가 있음을 알 수 있습니다. 내 routes.rb
에 namespace :api
이 path: '/'
으로 설정되어 있으므로 내 게시물 요청이 작동해야한다고 생각합니다.
누구든지 내가 잘못한 것을 지적 할 수 있습니까?
$ 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 여기
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
시험 것 :
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
012,351
여기 여기
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
내 경로입니다 내 routes.rb의
$ 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>'
감사합니다 : 6,
다음은 오류입니다!
아! 네가 맞아, 이걸 보니 너무 고마워! – Brian