简体   繁体   中英

Ruby On Rails - AJAX Call, Template Missing [edit - rendering html instead of js ]

Context - I have a web page, that says upload file. The file has questions in JSON format. When the file is uploaded, the questions should be inserted into a particular table. Once done, the page should show how many questions inserted and how many errored out.

Error : On uploading the file, I get a message - Template Missing

Missing template questions/upload_questions, application/upload_questions with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/tusharsaurabh/Ruby On Rails/Q/app/views"

I have checked the "Views" folder and the template is present.

Issue - How to resolve the Template Missing issue, even though it is present?

EDIT The application is searching for upload_questions.html.erb instead of upload_qustions.js.erb. How do I make the app render js.erb template instead of html.erb.

Code Snippets

View that shows the upload question button. 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

The models are, Title - questions has been categorized in different bucket. Each bucket is called Title. Question - individual questions.

Title

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');

Log Output 在此处输入图片说明

Snapshot of Rails folder -

在此处输入图片说明

When you submit the form you are making a POST request to the questions/upload_questions route which runs the QuestionsController#upload_questions action as you expect.

The problem is that this action is being requested with an HTML format which is the default and would result in Rails trying to render an upload_questions.html.erb template.

Since your intention is to render a JavaScript response in the form of the upload_questions.js.erb template, you need to request the route with the JS format .

To do this, you need to include the remote: true option on your form. This tells Rails to request a JS format response rather than an HTML one.

<%= form_tag '/questions/upload_questions', remote: true, multipart: true, class: "form-horizontal" do %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM