Hello, there is such a code in the controller:

before_action :set_question, only: [:show, :edit, :update, :destroy, :upvote,:downvote] def show end def update if @question.update(question_params) flash[:success] = "Question was successufly updated" redirect_to question_path(@question) else render 'edit' end end # DELETE /questions/1 # DELETE /questions/1.json def destroy @question.destroy flash[:success] = "Question was successfully destroyed." redirect_to questions_url end def set_question @question = Question.where(id: params[:id]).first unless @question render status: 404 end end 

And such a test

  def self.it_renders_404_page_when_question_is_not_found(*actions) actions.each do |a| it "#{a} renders 404 page when questions is not found" do verb = if a == :update "PATCH" elsif a == :destroy "DELETE" else "GET" end process a, verb, {id: 0} expect(response).to have_http_status(404) end end end it_renders_404_page_when_question_is_not_found :show, :edit, :update, :destroy 

On the show method, everything is fine, but on update and destroy there is such an error:

  Failure/Error: render status: 404 ActionView::MissingTemplate: Missing template questions/destroy, application/destroy with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :slim, :jbuilder]}. Searched in: * "/home/max/Desktop/stack/app/views" * "/home/max/.rvm/gems/ruby-2.2.2/gems/twitter-bootstrap-rails-3.2.2/app/views" * "/home/max/.rvm/gems/ruby-2.2.2/gems/devise-3.5.5/app/views" 

    1 answer 1

    Tests quite rightly fell. An application in such conditions would also fall.

    The description of the error is quite understandable and corresponds to the incorrectness of the logic:

      Failure/Error: render status: 404 # ищем, где такая строчка Missing template questions/destroy # замечаем, что этого шаблона и правда нет 

    Where is the connection? The default render behavior. What is not specified in his arguments will not be empty, but filled with what is supposed to be the default. And by default, the response body should be a view of the <контроллер>/<экшн> , which we observe.

    You apparently did not take my previous answer to this topic seriously. If you want to give "just 404" in Rails, all you have to do is create a situation in which at the right moment an ActiveRecord::RecordNotFound exception will ActiveRecord::RecordNotFound .

    Therefore, you can replace all your set_question with one line:

     @question = Question.find(params[:id]) 

    ... and he will already correctly respond to the situation when the question is not found.

    Do not fence the extra code where it is simply not needed.

    • If you replace the entire set_question with one line, then 4 errors occur - Couldn't find Question with 'id' = 0. If you replace with @question = Question.find_by_id (params [: id]), then first pops up 4 errors -undefined method authenticate', а после добавления в тест user = FactoryGirl.create(:user, role: "moder") sign_in user появляется ещё 4 ошибки: undefined method for example, it’s 200, it’s 200, undefined method `visitors' for nil: Maxim Cherevatov
    • If, however, in set_question again to return unless question render status: 404 end and leave it like this: question = Question.find_by_id (params [: id]) unless question render status: 404 end, then again the same errors - Missing template - Maxim Cherevatov
    • @MaximCherevatov and what? RecordNotFound, which gives "Couldn't find ..." is normal. Once again look at the answer link. - D-side
    • @MaximCherevatov why the missing template is obtained, I wrote. Did you read the answer at all? - D-side
    • Of course I read it, but I don’t understand what to do. Every time there are new errors. If not RecordNotFound, then Missing template. They seem to replace each other .. - Maxim Cherevatov