以前有人遇到过这个错误吗?我在网上查看过,找不到多少信息,也许我没有正确地呈现我的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我感谢你提前提供帮助。
发布于 2018-08-31 03:24:36
如下所示,控制器名为复数
在您的控制器目录下,您应该拥有以下内容
# app/controller/answers_controller.rb
class AnswersController < ApplicationController
def results
end
end现在,在视图目录中,视图命名空间应该与控制器名称相同。
# app/views/answers/results.html.erb
<h1>Check if this works</h1>routes.rb
resources :answers do
collection/member do
get :results
end
end发布于 2018-08-31 03:09:59
你的问题是你不回来了。Rails将为操作呈现相关的模板,除非您返回。尝试将您的render调用包装为return()。
附带注意,为什么要使用Array.wrap()?ActiveRecord的all方法类似数组。它们等待执行查询,直到您真正地尝试迭代它们,然后它们就像一个数组。你不应该让他们成为一个数组。如果您发现有,您可以调用.to_a对他们。
但是,关于活动记录集合,需要注意的一点是,如果从它的“数组”中删除任何内容,它实际上将从数据库中删除,这可能是您想要的,也可能不是您想要的。
https://stackoverflow.com/questions/52107827
复制相似问题