首页 > 解决方案 > JSON渲染缺少模板rails问题?

问题描述

有没有人遇到过这个错误?我在网上查了一下,找不到太多信息,也许我没有正确渲染我的 JSON?

缺少模板答案/结果,应用程序/结果与 {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}。在以下位置搜索:*“/Users/Minhaj/Desktop/my_survey/my_survey/app/views”*“/Users/Minhaj/.rvm/gems/ruby-2.4.1/gems/devise-4.5.0/app/views”

这是代码。如果有错误,将不胜感激帮助修复它。

  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

我提前感谢您的帮助。

标签: ruby-on-railsruby

解决方案


这是它的样子,控制器名称是复数

在你的控制器目录下,你应该有这个

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

现在在视图目录中,视图命名空间应该与控制器名称相同。

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

路线.rb

resources :answers do
  collection/member do
    get :results
  end
end

推荐阅读