2016-10-04 9 views
0

コンテキスト - 私はアップロードファイルを言うWebページを持っています。このファイルにはJSON形式の質問があります。ファイルがアップロードされると、質問は特定のテーブルに挿入する必要があります。完了すると、挿入された質問の数とエラーの数がページに表示されます。Ruby On Rails - AJAX呼び出し、テンプレートが見つかりません[編集 - jsの代わりにHTMLをレンダリング]

エラー:ファイルをアップロードするには、私がメッセージを取得する - テンプレートは、テンプレート質問/ upload_questions、アプリケーション/ upload_questionsと{行方不明

が欠落しています。locale => [:EN]:フォーマット=> [:html]、:variants => []、:ハンドラ=> [:erb、:ビルダー、:raw、:ruby、:coffee、:jbuilder]}。検索対象:* "/ Users/tusharsaurabh/Ruby On Rails/Q/app/views"

"Views"フォルダをチェックしてテンプレートが存在します。

問題 - テンプレート欠落問題の解決方法(存在していても)?

EDIT アプリケーションがupload_questions.js.erbではなくupload_questions.html.erbを検索しています。 html.erbではなくjs.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 <%[email protected]_questions%></li> 
    <li>Number of Question Failed <%[email protected]_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 "https://stackoverflow.com/questions/show_upload_screen" => 'questions#show_upload_screen' 
post "https://stackoverflow.com/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。RailsのフォルダのERB

$('#show_uploaded_question_summary').css('display','inline'); 

ログ出力 enter image description here

スナップショット -

enter image description here

+0

必要な名前のビューを作成するか、 'format.html'レスポンスを変更して別のページに移動することができます。ファイルをアップロードした後の目標は何ですか?基本的にコントローラのアクションに対するデフォルトのレスポンスは、別の方法で言わない限り、同じ名前のビューを探すことです。 – MageeWorld

+0

@MageeWorldのshow_upload_screen.html.erbには、「display:none;」という要素があります。プロパティ。ファイルがアップロードされると、upload_questions.js.erbはcssプロパティをインラインにします。それで全部です。ファイルupload_questions.js.erbはビューフォルダにあります。 –

答えて

0

あなたがQuestionsController#upload_questionsを実行しquestions/upload_questionsルートにPOST要求を行っているフォームを送信します期待どおりの行動。

問題は、このアクションがデフォルトで、Railsがupload_questions.html.erbテンプレートをレンダリングしようとしていることになるHTML形式で要求されていることです。あなたの意図は、あなたがJS形式でルートを要求する必要がupload_questions.js.erbテンプレートの形でのJavaScriptレスポンスをレンダリングすることなので

これを行うには、フォームにremote: trueオプションを含める必要があります。これは、HTMLではなく、JS形式の応答を要求するようにRailsに指示します。

<%= form_tag '/questions/upload_questions', remote: true, multipart: true, class: "form-horizontal" do %> 
関連する問題