【发布时间】:2016-12-29 07:16:47
【问题描述】:
我已经设置好路由,以便它们在我的控制器中按预期工作;我可以按预期使用 room_path 和 rooms_path。
但是,当我出于某种原因尝试在控制器规范中使用相同的路由时,会出现错误:
ActionController::UrlGenerationError: 没有路线匹配 {:action=>"/1", :controller=>"rooms"}
我的 routes.rb 文件:
root "rooms#index"
resources :rooms, :path => '/', only: [:index, :create, :show] do
resources :connections, only: [:create,:destroy]
end
如果我耙路线:
room_connections POST /:room_id/connections(.:format) connections#create
room_connection DELETE /:room_id/connections/:id(.:format) connections#destroy
rooms GET / rooms#index
POST / rooms#create
room GET /:id(.:format) rooms#show
但是我的测试失败了:
describe "GET room_path(room)" do
it "renders show" do
@room = Room.create
get room_path(@room)
expect(response.status).to eq(200)
expect(response).to render_template(:show)
end
end
虽然我的控制器可以毫无问题地使用相同的路由助手:
class RoomsController < ApplicationController
def index
end
def create
@room = Room.create
redirect_to room_path(@room)
end
def show
@room = Room.find(params[:id])
end
end
我不确定为什么在我的测试中它似乎在寻找“/1”动作而不是像我期望的那样寻找房间#show。
更新
所以继续玩这个我已经能够通过更改以下内容获得测试绿色:
describe "GET room_path(room)" do
it "renders show" do
@room = Room.create
get :show, params: { id: @room.id }
expect(response.status).to eq(200)
expect(response).to render_template(:show)
end
end
我仍然很想了解为什么我的助手不起作用。这是可以预料的吗?手动编写参数哈希是一种 PITA。
【问题讨论】:
标签: ruby-on-rails routes