我在routes.rb
文件中设置了以下资源:
namespace :api, path: '/', defaults: { format: 'json' }, constraints: { subdomain: 'api' } do
namespace :v1 do
resources :yums do
resources :likes, only: [:create, :destroy, :index] do
match '/', to: 'likes#destroy', via: 'delete'
end
end
end
end
我还写了一个测试我的请求的规范:
describe "Like API requests" do
before (:each) do
host! 'api.example.com'
@user = FactoryGirl.create(:user)
@other_user = FactoryGirl.create(:user)
@yum = FactoryGirl.create(:yum, user: @other_user)
end
describe "liking a Yum" do
it "should increase the Yum's Like count" do
expect do
post "/v1/yums/#{@yum.id}/likes", { authorization: token_header(@user.auth_token) }
end.to change(@yum.reload.likes, :count).by(1)
end
end
describe "unliking a Yum" do
it "should increase the Yum's Like count" do
expect do
delete "/v1/yums/#{@yum.id}/likes", { authorization: token_header(@user.auth_token) }
end.to change(@yum.reload.likes, :count).by(-1)
end
end
end
我希望Likes
控制器不需要破坏操作的ID,因为我在我的用户模型上使用方法喜欢和不同,但这个方案似乎不起作用,我是什么做错了?
答案 0 :(得分:1)
尝试在routes.rb中添加路线,如:
match "v1/yums/:yum_id/likes", to: "likes#destroy", via: "delete", defaults: { id: nil }
答案 1 :(得分:0)
尝试将defaults: { id: nil }
选项传递给match()
:
match '/v1/yums/:yum_id/likes', to: 'likes#destroy', via: 'delete', defaults: { id: nil }