我知道在SO上有很多类似的问题,但我还没找到一个适合我想做的事情......(因为我并不是100%肯定它是甚至可以做我想做的事情!)
目前我有一个页面根据有多少'问题创建了几个form_tags。有'测试',就像这样:
<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>
目前我已禁用“提交”#39;按钮,因为我不希望表单实际提交,直到所有表单都完成,所以我有这个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();
});
});
});
它确实尝试提交答案,但仍尝试将用户带到编辑页面,但有几个[:id],显然不起作用......
在我的TestsessionsController中我有这个:
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
有没有办法强制最后一个链接直接将其带到结果页面,但仍然可以从form_tags提交信息?
目前它只是给我这个错误:
No route matches [POST] "/tests/1/testsessions/%23%3CTestsession::ActiveRecord_Associations_CollectionProxy:0x007fe197a72e40%3E/edit"
只有当我点击后退按钮时才会将我带到结果页面...
修改
配置/路由文件:
Rails.application.routes.draw do
devise_for :users
root 'welcome#index'
get 'tests/:id/testsessions/new' => 'testsessions#create'
get 'tests/:id/testsessions/results' => 'testsessions#results', as: :results
resources :tests do
resources :questions
resources :testsessions
end
end
答案 0 :(得分:0)
更新我认为你的意思是:
<%= form_tag edit_test_testsession_path(@test, @testsession), id: 'submit_answer' do %>
问题似乎在这里:
<%= form_tag edit_test_testsession_path(@test, question.testsessions), id: 'submit_answer' do %>
您正在传递所有相关测试会话的集合而不是单个对象,我怀疑这是您打算做的。
请显示与控制器相关的config / routes.rb的一部分