上下文 - 我有一个网页,上面写着上传文件。该文件有JSON格式的问题。上传文件时,应将问题插入特定表中。完成后,页面应显示插入的问题数量和错误数量。
错误:在上传文件时,我收到一条消息 - 模板缺失
缺少模板问题/ upload_questions,application / upload_questions {{locale => [:en],:formats => [:html],:variants => [],:handlers => [:erb ,:builder,:raw,:ruby,:coffee,:jbuilder]}。搜索:*“/ Users / tusharsaurabh / Ruby On Rails / Q / app / views”
我已查看“视图”文件夹,并且模板已存在。
问题 - 如何解决模板缺失问题,即使它存在?
修改 该应用程序正在搜索upload_questions.html.erb而不是upload_qustions.js.erb。如何使应用程序渲染js.erb模板而不是html.erb。
代码段
显示上传问题按钮的视图。的 show_upload_screen.html.erb
<div class="jumbotron" style="margin:20% 30% 30% 30%;background: linear-gradient(15deg, #ff471a, #ffff00);border-radius:10px;display:flex;">
<%= form_tag '/questions/upload_questions',multipart: true, class: "form-horizontal" do %>
<div class="form-group">
<%= file_field_tag "file",id:"file_tag", type: "hidden",class: "btn btn-primary control-label col-md-8" %>
<%= submit_tag 'Process',id:"submit_tag",class: "btn btn-primary control-label col-md-4" %>
</div>
<% end %>
</div>
<div class="well" id="show_uploaded_question_summary" style="margin-top:10px;display:none">
<ul>
<li>Number of Question Updated <%=@updated_questions%></li>
<li>Number of Question Failed <%=@errored_questions%></li>
</ul>
</div>
的routes.rb
root 'signup#validate'
post "/signup/verify" => 'signup#verify'
get "/signup/verify" => 'signup#verify'
get "/questions_controller/show_question" => "questions#show_question"
get "/questions_controller/check_answers" => "questions#check_answers"
get "/signup/create_user" => 'signup#create_user'
post "/signup/create_user" => 'authenticates#create_user'
get "/questions/show_upload_screen" => 'questions#show_upload_screen'
post "/questions/upload_questions" => 'questions#upload_questions'
questions_controller.rb
def upload_questions
@errored_questions = 0
@updated_questions = 0
tempfl = params[:file]
question_array = JSON.parse(tempfl.tempfile.read)
question_array.each do |question_hash|
logger.debug "the hash i am trying is #{question_hash}"
id_rel = Title.get_title_id(question_hash["title"])
if id_rel.nil?
id = Title.insert_title(question_hash["title"])
else
id = id_rel.id
end
question_updated = Title.update_questions(id, question_hash)
logger.debug "the return is #{question_updated}"
if question_updated == "Not Saved"
@errored_questions += 1
else
@updated_questions += 1
end
end
logger.debug "the data value is #{@errored_questions} #{@updated_questions}"
respond_to do |format|
format.js { render 'upload_questions.js.erb' }
format.html
end
end
模型是,标题 - 问题已被分类在不同的桶中。每个桶称为标题。问题 - 个别问题。
标题
class Title < ActiveRecord::Base
has_many :questions
def self.get_all_title
title_all = Title.all
end
def self.get_title_id title_name
title_rel = Title.find_by title: title_name
end
def self.insert_title title_name
title_rec = Title.create(title: title_name)
end
def self.update_questions id, my_hash
title = Title.find id
title.questions.create(question: my_hash["question"],option1: my_hash["options"][0],option2: my_hash["options"][1],option3: my_hash["options"][2],option4: my_hash["options"][3],answer: my_hash["answer"])
rescue => e
logger.debug "Error: Creating the data #{my_hash} #{e.full_messages}"
return "Not Saved"
end
end
Question.rb
class Question < ActiveRecord::Base
belongs_to :title
def self.get_saved_answer(question_id)
result = Question.find_by id: question_id
result['answer']
end
end
upload_questions.js.erb
$('#show_uploaded_question_summary').css('display','inline');
Rails文件夹快照 -
答案 0 :(得分:0)
当您提交表单时,您正在向skipped=0
路由发出POST
请求,该请求按预期运行questions/upload_questions
操作。
问题是,正在使用QuestionsController#upload_questions
格式请求此操作,这是默认操作,会导致Rails尝试呈现HTML
模板。
由于您的目的是以upload_questions.html.erb
模板的形式呈现 JavaScript 响应,您需要请求具有JS格式的路径。
为此,您需要在表单中包含upload_questions.js.erb
选项。这告诉Rails请求remote: true
格式响应而不是JS
响应。
HTML