I'm still making online test program.
这是我的模型。
调查
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)不能显示任何结果:“未定义的方法‘`name’for nil:NilClass”。
result.html.erb
<h1><%= @survey.name %></h1>
<h3><%= @survey.user.username %></h3>正如我在previous link中所说的,在调查视图中,我在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)这是耙路的结果。
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="}这样就可以了。但是由于form_tag在show.html.erb中,显示页面包含另一个输入表单。
<%= 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'你认为我需要改变她的回答机制吗?
发布于 2013-08-15 08:12:28
尝试这样做,您正在尝试调用数组上的方法。您必须对其进行迭代,然后可以将任何值分配给它。您也可以在代码中进行一些重构,以避免循环。
在你的评分方法中,这样做:
def grading
@survey = Survey.new
@survey.user_id = current_user.id
@survey.questions.each do |question|
question.auto_check
end
redirect_to results_surveys_path(@survey)
end在您的问题模型中,编写如下方法auto_check:
def auto_check
answers.each do |answer|
is_correct = true if answer.user_answer and answer.correct
self.save!
end
end我认为这是一个更好的approach.Thanks。
最新答复:
每当您试图在路径中传递一个id时,这意味着它是一个成员函数,如果您在路由中定义集合,这意味着您不能在其中传递一个id (如您的路由输出所示)。对此有敏锐的了解。像这样改变你的路线:
resources :surveys do
member do
get 'results'
end
end现在你可以这样访问你的网址了:
results_survey_path(@survey)在您的results方法中:
def results
@survey = Survey.where(params[:id])
end希望,现在它会成功的。
https://stackoverflow.com/questions/18247863
复制相似问题