1
votes

Has anyone come across this error before? I've looked online and couldn't find much information, perhaps I didn't render my JSON correctly?

Missing template answer/results, application/results with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/Minhaj/Desktop/my_survey/my_survey/app/views" * "/Users/Minhaj/.rvm/gems/ruby-2.4.1/gems/devise-4.5.0/app/views"

Here is the code. If there is an error, help will be appreciated in fixing it.

  def create
    @answer = current_survey.answers.create(question_id: question, choice_id: choice)
    redirect_to question_survey_path(current_survey)
  end

  def results
    @choices = choices_hash
    @results = questions.map do |question|
      {
        question.question => CountChoicesService.call(question.id)
      }
    end
    respond_to do |format|

      format.html { render :nothing => true, :status => 204 }
      format.json { render json: @results }

    end
  end

  private

  def question
    @question ||= params[:question]
  end

  def choice
    @choice ||= params[:choice]
  end

  def choices
    @choices ||= Array.wrap(Choice.all)
  end

  def choices_hash
    choices_hash = {}
    choices.each { |choice| choices_hash[choice.id] = choice.choice }
    choices_hash
  end

  def questions
    @questions ||= Array.wrap(Question.all)
  end
end

I appreciate the help in advance.

2
You need to create a view named results.html.erb under answers namespace. You should have a directory structure like this app/views/answers/, go inside that and create results.html.erb - Kedarnag Mukanahallipatna
Missing template answer/results. This means either you need to redirect in the results action, or create a view like I explained above. Usually, we redirect on success. - Kedarnag Mukanahallipatna
I have a page for results.html.erb under answers directory like you said. How do I redirect however. Also what exactly do you mean by redirecting? - user10294084
There is a typo, your directory name is not answers it's answer.Change it to answers - Kedarnag Mukanahallipatna
the name of my directory is answers. There is no typo. - user10294084

2 Answers

0
votes

Here is how it should look like, controller name is plural

Under your controller directory, you should have this

# app/controller/answers_controller.rb
class AnswersController < ApplicationController
  def results
  end
end

Now in the views directory,the view namespace should be the same as the controller name.

# app/views/answers/results.html.erb
<h1>Check if this works</h1>

routes.rb

resources :answers do
  collection/member do
    get :results
  end
end
-1
votes

Your problem is that you are not returning. Rails will render the associated template for the action unless you return. Try wrapping your render calls in return().

Side note, why are you using Array.wrap()? ActiveRecord’s all methods are array-like. They wait to execute the query until you actually try to iterate them, and then they act like an array. You shouldn’t need to make them an array. If you ever find that you do, you can call .to_a on them.

One thing to note about the active record record collection though, if you remove anything from its “array”, it will actually do a delete from the database, which may or may not be what you want.