构建JSON,我不需要":new"和":编辑/:id"路线。 所以我要求rails不要创建那些但是当我指定不应该可路由的路由时(使用Be_routable匹配器)......它不起作用。 有什么建议吗?
routes.rb:
Rails.application.routes.draw do
resources :todos, :except => [:edit, :new]
end
routes_spec.rb:
require 'spec_helper'
RSpec.describe "routes for Todos", :type => :routing do
it "routes get /todos to the todos controller index action" do
expect(:get => "/todos").
to route_to(:controller => "todos", :action => "index")
end
it "routes post /todos to the todos controller create action" do
expect(:post => "/todos").
to route_to(:controller => "todos", :action => "create")
end
it "routes get /todos/:id to the todos controller show action" do
expect(:get => "/todos/:id").
to route_to(:controller => "todos", :action => "show", :id => ":id")
end
it "routes patch /todos/:id to the todos controller update action" do
expect(:patch => "/todos/:id").
to route_to(:controller => "todos", :action => "update", :id => ":id")
end
it "routes put /todos/:id to the todos controller update action" do
expect(:put => "/todos/:id").
to route_to(:controller => "todos", :action => "update", :id => ":id")
end
it "routes delete /todos/:id to the todos controller delete action" do
expect(:delete => "/todos/:id").
to route_to(:controller => "todos", :action => "destroy", :id => ":id")
end
it "does not route to /todos/new" do
expect(:get => "/todos/new").not_to be_routable
end
end
最后一次阻止抛出此错误:
Failures:
1) routes for Todos does not route to /todos/new
Failure/Error: expect(:get => "/todos/new").not_to be_routable
expected {:get=>"/todos/new"} not to be routable, but it routes to {:controller=>"todos", :action=>"show", :id=>"new"}
# ./spec/routing/routes_spec.rb:43:in `block (2 levels) in <top (required)>'
所有其他阻止都可以
todos_controller.rb:
class TodosController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
def create
@todo = Todo.new(todo_params)
if @todo.save
render json: @todo, status: :created, location: @todo
else
render json: @todo.errors, status: :unprocessable_entity
end
end
def show
@todo = Todo.find(params[:id])
render json: @todo, status: :ok, location: @todo
end
def index
@todos = Todo.all
render json: @todos, status: :ok #, location: @todos DOESN'T WORK 'location:' OPTION
end
def update
@todo = Todo.find(params[:id])
if @todo.update(todo_params)
render json: @todo, status: :ok, location: @todo
else
render json: @todo.errors, status: :unprocessable_entity
end
end
def destroy
@todo = Todo.destroy(params[:id])
render json: @todo, status: :no_content, location: @todo
end
private
def todo_params
params.require(:todo).permit(:title, :content)
end
def not_found(e)
render :json => { :message => e.message }, :status => :not_found
end
end
答案 0 :(得分:1)
您收到错误的原因是您无法使用此测试。
原因是这条路线:
/待办事项/:ID
是路由到SHOW操作的路由。
所以当你浏览
时/待办事项/新
':id'的值将为'new'。
您可以通过在show方法中添加'puts params [:id]'来检查这一点,然后在浏览器转到'/ todos / new'中,您将在日志中看到它将显示'new'。
您应该添加路线约束。例如:
get 'todos/:id' => 'todos#show', constraint: { id: /\d+/ }
我强烈建议您阅读和理解的更多信息here。 :)
编辑:约束限制param:id,以便只允许整数。