ruby-on-rails - 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
我提前感谢您的帮助。
解决方案
这是它的样子,控制器名称是复数
在你的控制器目录下,你应该有这个
# 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
推荐阅读
- python - 不断收到“找不到页面”。该怎么办?
- 3dsmax - 如何通过 Python 重置资产路径
- python - 如何将 pytorch 添加到包含用 C++ 编写的项目的 docker 文件中?
- vue.js - 如何在 Nuxt 中组件和页面的脚本部分访问 vuex
- c++ - c++中的内存泄漏示例
- swift - 如何在 SwiftChart 中为 x 轴使用日期?
- reactjs - 为什么带有 Context Provider 的组件不会重新渲染
- javascript - 如何告诉 JSDoc 我正在扩展具有特定类型的模板类?
- python - F.write 已返回 'io.UnsupportedOperation: not writable' 我不知道为什么
- c# - 如何在使用 C# 遍历 XML 文档(XML 解析)时提取特定属性