隐藏字段将所有参数作为字符串传递

时间:2017-12-08 21:37:01

标签: ruby-on-rails parameter-passing

我有一个名为“问题”的控制器,有两个动作,“练习”和“等级”都是获取路线,练习视图呈现一小部分问题,并使用表格通过提交来获取用户答案“等级路径“

要对它进行评分,我需要将记录对象作为数组传递,但是当使用隐藏字段传递时,我将引用数字作为字符串而不是实际对象。我如何获得该对象,以便我可以调用question_object.answer

控制器/ questions.rb

def practice
   @questions = Questions.find(params[:id])
   @sample_array =  @questions.multiple_choices.sample(params[:number_of_multiple_choices].to_i)
   @sample_array += @questions.true_falses.sample(params[:number_of_true_falses].to_i)
   @user_answers = Array.new(@questions.size, 1)
end

def grade
   @questions = Questions.find(params[:id])
   @sample_array = params[:sample_array]
   @array_answers = # create array based on sample_array's answers
   @user_answers = params[:user_answers].split(' ')
   @user_answers = @user_answers.map.with_index{|x,y| params[("user_answer#{y.to_s}")] }
end

views / questions / practice.html.erb#不介意有限数量的表格,例如。

<%= form_for :grade, :url => grade_path, method: :get do |f| %>
  <%= hidden_field_tag(:sample_array, @sample_array) %>
  <%= hidden_field_tag(:user_answers, @user_answers) %>

<div class="form-group">
  <%= select_tag(("user_answer#{0}"), options_for_select([["a", 1],[ "b", 2],[ "c", 3],[ "d", 4]], 1), class: "form-control") %>
  <%= select_tag(("user_answer#{1}"), options_for_select([["a", 1],[ "b", 2],[ "c", 3],[ "d", 4]], 1), class: "form-control") %>
  <%= select_tag(("user_answer#{2}"), options_for_select([["a", 1],[ "b", 2],[ "c", 3],[ "d", 4]], 1), class: "form-control") %>
  <%= select_tag(("user_answer#{3}"), options_for_select([["a", 1],[ "b", 2],[ "c", 3],[ "d", 4]], 1), class: "form-control") %>
</div>
<div id="finalscore">
  <%= button_to "act_2", class:'btn btn-success' %>
</div>
<% end %>

在等级中,我传递的参数只是字符串。问题:[“#”,“#”,“#”]

我研究了它,hidden_​​fields将传递query_strings,这就是我只获取字符串的原因,但我找不到任何关于如何获取数组对象以在我的成绩控制器操作中使用的示例。

提前感谢抱歉,我不知道如何提出这个问题。

1 个答案:

答案 0 :(得分:0)

为什么不在practice方法中创建数组,而不是在grade方法中创建数组?为此,您可以在@number_of_multiple_choice方法中指定@number_of_true_falsepractice,在隐藏字段标记中传递这些数字,并在grade操作中确定数组。我实际上不确定您在@user_answers操作中使用practice尝试做什么 - 我假设您只是尝试创建一个空的答案对象,但它似乎没有被使用那种形式。

<强>控制器/ questions.rb

def practice
   @question = Question.find(params[:id])
   @number_of_multiple_choices =             params[:number_of_multiple_choices].to_i
   @number_of_true_falses = params[:number_of_true_falses].to_i
end

def grade
 @question = Question.find(params[:id])
 @sample_array =   @questions.multiple_choices.sample(params[:number_of_multiple_choices].to_i)
 @sample_array += @questions.true_falses.sample(params[:number_of_true_falses].to_i)
 @array_answers = # create array based on sample_array's answers
 @user_answers = params[:user_answers].split(' ')
 @user_answers = @user_answers.map.with_index{|x,y| params[("user_answer#{y.to_s}")] }
end

<强>视图/问题/ practice.html.erb

<%= form_for :grade, :url => grade_path, method: :get do |f| %>
  <%= hidden_field_tag(:number_of_multiple_choice, @number_of_multiple_choice) %>
  <%= hidden_field_tag(:number_of_true_falses, @number_of_true_falses) %>
  ...
<% end %>

此外,我注意到你在说@questions = Questions.find(params[:id].find意味着返回一条记录,但你有a)复数你的变量表明你想要多个问题,b)复数你的模型,这不是标准惯例。您的模型应该重命名,或者如果您的模型实际上不是多元化,则会抛出错误。