我在测试ratings_controller时遇到了这些错误。ActiveRecord :: RecordNotFound:无法找到没有ID的酒店
1) RatingsController create action creates a rating if validations pass
Failure/Error: post :create, rating: {value: 4, user_id: user, hotel_id: hotel}
ActiveRecord::RecordNotFound:
Couldn't find Hotel without an ID
# ./app/controllers/ratings_controller.rb:6:in `create'
# ./spec/controllers/ratings_controller_spec.rb:12:in `block (4 levels) in <top (required)>'
# ./spec/controllers/ratings_controller_spec.rb:11:in `block (3 levels) in <top (required)>'
2) RatingsController create action does not create rating if validations fail
Failure/Error: post :create, rating: {value: 3}
ActiveRecord::RecordNotFound:
Couldn't find Hotel without an ID
# ./app/controllers/ratings_controller.rb:6:in `create'
# ./spec/controllers/ratings_controller_spec.rb:17:in `block (3 levels) in <top (required)>'
3) RatingsController update action updates rating if validations ok
Failure/Error: patch :update, value: 3, user_id: user.id, hotel_id: hotel.id
ActionController::UrlGenerationError:
No route matches {:action=>"update", :controller=>"ratings", :hotel_id=>"1", :user_id=>"2", :value=>"3"}
# ./spec/controllers/ratings_controller_spec.rb:25:in `block (3 levels) in <top (required)>'
我不知道它们来自哪里。如果可以,请帮助我。 我ratings_controller:
class RatingsController < ApplicationController
#before_action :signed_in_user
before_filter :authenticate_user!
def create
@hotel = Hotel.find(params[:hotel_id])
@rating = Rating.new(params[:rating])
@rating.hotel_id = @hotel.id
@rating.user_id = current_user.id
if @rating.save
redirect_to hotel_path(@hotel), :notice => "Your rating has been saved"
end
end
def update
@hotel = Hotel.find(params[:hotel_id])
#@rating = current_user.ratings.find(@hotel.id)
@rating = Rating.find_by_hotel_id(@hotel.id)
if @rating.update_attributes(params[:rating])
redirect_to hotel_path(@hotel), :notice => "Your rating has been updated"
end
end
end
我ratings_controller_spec.rb:
require "spec_helper"
describe RatingsController do
let(:rating) { FactoryGirl.create(:rating) }
let(:user) { FactoryGirl.create(:user) }
let(:hotel) { FactoryGirl.create(:hotel) }
describe "create action" do
before { sign_in rating.user }
it "creates a rating if validations pass" do
expect {
post :create, rating: {value: 4, user_id: user, hotel_id: hotel}
}.to change(Rating, :count).by(1)
end
it "does not create rating if validations fail" do
post :create, rating: {value: 3}
expect(response).to redirect_to(hotel_path(hotel))
end
end
describe "update action" do
before { sign_in hotel.user }
it "updates rating if validations ok" do
patch :update, value: 3, user_id: user.id, hotel_id: hotel.id
rating.reload
expect(rating.value).to eq(3);
end
it "updates rating if validations fail" do
end
end
end
尤其是第三个错误困惑我,因为rake routes
显示了评级更新动作我avaible路线。
PATCH /hotels/:id(.:format) hotels#update
PUT /hotels/:id(.:format) hotels#update
谢谢!
Rails - 4.0.8
Ruby - 1.9.3p551
UPDATE 1: 对不起路由。我的mystake。只是复制了错误的行。
rating PATCH /ratings/:id(.:format) ratings#update
PUT /ratings/:id(.:format) ratings#update
似乎对我来说很好,如果我启动服务器并手动测试它,它工作正常。
谢谢你的回答。对路线抱歉。我复制了错误的行。请检查出这个帖子。 –
更新是否能解决您的问题? – p4sh4