这是我的模型。
民意调查
class Survey < ActiveRecord::Base
belongs_to :user
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda {|a| a[:content].blank?}, :allow_destroy => true
问题:有 is_correct 列表示学生是否得到正确答案。
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
答案:有教师检查进行调查(测试)的正确列,并且有学生标记参加测试的 user_answer 列。
class Answer < ActviveRecord::Base
belongs_to :question
我在调查视图中的 show.html.erb 上制作了考试界面。因此,如果学生填写此复选框并单击提交按钮,他们可以获得他们的结果页面。但我无法在结果页面中显示结果。
这是我的调查控制器。
def grading
@survey = Survey.new
@survey.user_id = current_user.id
if @survey.questions.answers.user_answer and @survey.questions.answers.correct
@survey.questions.is_correct = true
end
redirect_to results_surveys_path(@survey)
end
def results
end
我看到的错误消息是 []:ActiveRecord::Relation 的“未定义方法“答案”。我以为问答表之间有问题...
我认为自动评分部分很容易,但我错了。我对此一无所知,除了您的帮助外,我没有任何参考。
欢迎任何想法。
谢谢先进。
更新的问题
这是另一个问题。
现在我可以访问嵌套对象。(我认为并且希望)但结果页面(调查视图中的result.html.erb)无法显示任何结果:“nil:NilClass 的未定义方法‘名称’”。
结果.html.erb
<h1><%= @survey.name %></h1>
<h3><%= @survey.user.username %></h3>
正如我在上一个链接中所说,我在调查视图中的 show.html.erb 中有另一个 form_tag。然后使用 routes.rb 重定向到结果页面。
resources :surveys do
collection do
get 'results'
end
end
我想我可以使用问题表中的 is_correct 列显示结果页面。
我没有在调查控制器的结果方法中写任何东西。因为当我重定向我这样写的页面时。这意味着在结果方法中使用@survey,不是吗?
redirect_to results_surveys_path(@survey)
这是 rake 路由的结果。
seriousin@classcasts:~/ClassCasts$ rake routes | grep survey
results_surveys GET /surveys/results(.:format) surveys#results
surveys GET /surveys(.:format) surveys#index
POST /surveys(.:format) surveys#create
new_survey GET /surveys/new(.:format) surveys#new
edit_survey GET /surveys/:id/edit(.:format) surveys#edit
survey GET /surveys/:id(.:format) surveys#show
PUT /surveys/:id(.:format) surveys#update
DELETE /surveys/:id(.:format) surveys#destroy
surveys_grading POST /surveys/grading(.:format) surveys#grading
seriousin@classcasts:~/ClassCasts$
我认为我的基本想法导致了我所有的问题。这是我的调查控制器。
class SurveysController < ApplicationController
def index
@surveys = Survey.all
end
def grading
@survey = Survey.new
@survey.user_id = current_user.id
@survey.questions.each do |question|
question.auto_check
end
redirect_to results_survey_path(@survey)
end
def results
@survey = Survey.where(params[:id])
end
def show
@survey = Survey.find(params[:id])
end
def new
@survey = Survey.new
end
def edit
@survey = Survey.find(params[:id])
end
def create
@survey = Survey.new(params[:survey])
@survey.user_id = current_user.id
end
def update
@survey = Survey.find(params[:id])
end
def destroy
@survey = Survey.find(params[:id])
@survey.destroy
end
end
如您所见,我在调查视图中使用显示页面作为另一种带有评分方法的输入表单。我可以在创建方法中使用'@survey = Survey.new',这很有意义!但正如我在评分方法中所写的那样,我认为它会产生另一个新的调查。所以我需要改变那条线。你能帮我么?
发送数据
好的。当我在调查视图中提交 _form.html.erb 时,我可以发送这样的数据。
Parameters: {"id"=>"14", "survey"=>{"name"=>"The First Test!", "description"=>"This is the first test!", "questions_attributes"=>{"0"=>{"_destroy"=>"false", "id"=>"41", "content"=>"Question 2", "answers_attributes"=>{"0"=>{"_destroy"=>"false", "id"=>"66", "content"=>"Answer 2 of Question 2", "correct"=>"0"}, "1"=>{"_destroy"=>"false", "id"=>"67", "content"=>"Answer 1 of Question 2", "correct"=>"1"}}}, "1"=>{"_destroy"=>"false", "id"=>"42", "content"=>"Question 1", "answers_attributes"=>{"0"=>{"_destroy"=>"false", "id"=>"68", "content"=>"Answer 2 of Question 1", "correct"=>"0"}, "1"=>{"_destroy"=>"false", "id"=>"69", "content"=>"Answer 1 of Question 1", "correct"=>"1"}}}, "1376575795482"=>{"_destroy"=>"false", "content"=>"Question 3", "answers_attributes"=>{"1376575802879"=>{"_destroy"=>"false", "content"=>"Answer 1 of Question 3", "correct"=>"0"}}}}}, "commit"=>"Update Survey", "utf8"=>"✓", "authenticity_token"=>"/vNuB5Ck3QM5p+5ksL3tlmb+ti5nTA/qS96+vbPQkNw="}
还行吧。但由于 show.html.erb 中的 form_tag,显示页面包含另一个输入表单。
<%= form_tag({:controller => "surveys", :action => "grading"}) do %>
在 show.html.erb 中再次提交后,我想以正确的结果重定向到 results.html.erb。但有这样的错误。
Started POST "/surveys/grading" for 110.174.136.30 at Thu Aug 15 23:19:52 +0900 2013
Processing by SurveysController#grading as HTML
Parameters: {"utf8"=>"✓", "#<Answer:0x7fafd8c5f5a0>"=>"1", "#<Answer:0x7fafd95704a0>"=>"0", "#<Answer:0x7fafd9116a58>"=>"1", "authenticity_token"=>"/vNuB5Ck3QM5p+5ksL3tlmb+ti5nTA/qS96+vbPQkNw=", "#<Answer:0x7fafd8d03a38>"=>"0", "commit"=>"Submit", "#<Answer:0x7fafd8cfc580>"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 12 LIMIT 1
Completed 404 Not Found in 3ms
ActionController::RoutingError (No route matches {:action=>"results", :id=>#<Survey id: nil, name: nil, description: nil, attempts: nil, user_id: 12, created_at: nil, updated_at: nil>, :controller=>"surveys"}):
app/controllers/surveys_controller.rb:31:in `grading'
您认为我需要更改 whloe 应答机制吗?