我正在使用Rails 4.2.5,我已经为destroy动作编写了一个控制器测试,我正在使用ajax调用来销毁和使用destroy.js.erb文件。当我调用js格式时,请帮我解决以下问题以通过测试,我在下面粘贴错误。
def destroy
@status = @song.destroy
respond_to do |format|
format.js
end
end
SongsControllerTest#test_should_destroy_song:
ActionController::UnknownFormat: ActionController::UnknownFormat
app/controllers/songs_controller.rb:36:in `destroy'
songs_controller_test.rb
test "should destroy song" do
assert_difference('Song.count', -1) do
delete :destroy, id: @song
end
get :destroy, format: 'js'
end
destroy.js.erb
var element = document.getElementById("<%="song#{@song.id}" %>");
<%if @status%>
element.parentNode.parentNode.remove();
<%end%>
答案 0 :(得分:3)
destroy
控制器操作通常会对DELETE
HTTP方法做出反应,因此在调用format
时应使用delete
选项:
test "should destroy song" do
assert_difference('Song.count', -1) do
delete :destroy, id: @song, format: :js
end
end