samedi 28 février 2015

Prevent form being submitted until final button - avoid edit page

I know there are a lot of similar questions on SO but I haven't found one that fits what I'm trying to do...(because I'm not 100% sure it is even possible to do what I am trying to do!!)


At the moment I have a page that creates several form_tags based on how many 'questions' there are in the 'test', like this:



<div class="jumbotron">
<% @test.questions.each do |question| %>
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Question <%= question.question %></h3>
</div>
<div class="panel-body">
<p>
<%= image_tag question.image.url(:medium) %>
</p>
<%= form_tag edit_test_testsession_path(@test, question.testsessions), id: 'submit_answer' do %>
<%= radio_button_tag :answer, "A" %> A
<%= radio_button_tag :answer, "B" %> B
<%= radio_button_tag :answer, "C" %> C
<%= radio_button_tag :answer, "D" %> D
<%= submit_tag 'Submit', class: "btn btn-success", id: 'single_submit' %>
<% end %>
</div>
</div>
<% end %>
<br/>
<%= link_to "See Test Results!", results_path(@test), class: "btn btn-lg btn-info btn-block", id: "submit_all" %>
</div>


At the moment I have disabled the 'submit' buttons because I don't want the forms to actually be submitted until all of them are completed so I have this Javascript:



$(document).ready(function() {
$('.btn.btn-success').click(function(e) {
e.preventDefault();
this.value="Resubmit";
});

$('#submit_all').click(function(){
$('#submit_answer').each(function(){
$(this).submit();
});
});
});


It does attempt to submit the answers but still tries to take the user to an edit page, but of several [:id], which obviously doesn't work...


In my TestsessionsController I have this:



class TestsessionsController < ApplicationController

def new
@test = Test.find(params[:test_id])
@testsession = Testsession.new
redirect_to action: :create
end

def create
@test = Test.find(params[:test_id])
@test.questions.each do |question|
@test.testsessions.create user: current_user, question: question
end
redirect_to action: :index
end

def index
@test = Test.find(params[:test_id])
@questions = @test.questions
# @testsession = Testsession.find(params[:id])
end

def show
@testsession = Testsession.find(params[:id])
@test = Test.find(params[:test_id])
@questions = @test.questions
end

def update
@testsession = Testsession.find(params[:id])
# @question = Question.find(params[:question_id])
@testsession.update(params.require(:testsession).permit(:answer))
redirect_to action: :results
end

def edit
@testsession = Testsession.find(params[:id])
@question = Question.find(params[:question_id])
@testsession.update(params.require(:testsession).permit(:answer))
redirect_to action: :results
end

def results
@test = Test.find(params[:id])
@testsession = Testsession.find(params[:id])
end

end


Is there any way to force the last link to take it directly to the results page, but still submit the information from the form_tags?


At the moment it is just giving me this error:



No route matches [POST] "/tests/1/testsessions/%23%3CTestsession::ActiveRecord_Associations_CollectionProxy:0x007fe197a72e40%3E/edit"


and only when I hit the back button it takes me to the results page...


Aucun commentaire:

Enregistrer un commentaire