获取ruby中的单选按钮值

时间:2013-10-15 12:06:54

标签: ruby-on-rails ruby

我创建了一个测验和显示,每页一个问题作为部分形式,当我使用创建时,我想要在创建之前检查的单选按钮的值。当我在我的控制器中使用params [:answer]时,它返回null ..任何人都可以帮助我获取我检查的单选按钮的值

提前致谢

<div class = "y">
<% form_for @answer do |f|%>
<div class = "label_field_pair">
<label for "questions">
  <%= @ans.ques %>
</label>  
</div> <br>
<div class = "label_field_pair2">
<label for "options">
  <div id = "option-1">
    <%= radio_button_tag "answer", "#{@ans.id}ans1"%><%= @ans.ans1 %>
  </div><br>
  <div id = "option-2">
    <%= radio_button_tag "answer", "#{@ans.id}ans2"%><%= @ans.ans2 %>
  </div><br>
  <div id = "option-3">
    <%= radio_button_tag "answer", "#{@ans.id}ans3"%><%= @ans.ans3 %>
  </div><br>
  <div id = "option-4">
    <%= radio_button_tag "answer", "#{@ans.id}ans4"%><%= @ans.ans4  %>
  </div><br>
</label>
  </div>
 <div class="next">
  <%= link_to_remote "Next", 
    :before => "Element.show('loader')",
    :success => "Element.hide('loader')",
    :url=>{:controller=>"answers", :action=>"next"},
    :with => "'&passed_question=#{@ans.id}&'+'&exam_group_id=#{@exam_group.id}&'"
  %> 
<% end %>
 </div> 
<% end %> 
</div> 

并在控制器中

def next
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find_by_id(params[:exam_group_id])
@answer = Answer.new(params[:ans])
@answer.answer = params[:answer]
@answer.exam_group_id = @exam_group.id
@answer.user_id = @user.id
passed_question = params[:passed_question]
@answer.questions_id = passed_question
if @answer.save
  @ans = Question.find_by_id(left_random, :conditions => ['id not in (?) && exam_group_id=?',answered, @exam_group])
    render(:update) do |page|
      page.replace_html 'main', :partial => 'ans', :object => @ans
    end
end
end
def ans
@user = current_user
@student = Student.find_by_admission_no(@user.username)
@exam_group = ExamGroup.find(params[:exam_group_id])
@tot_ans = answered.count
@last_ques = (Question.count-1)
render(:update) do |page|
  page.replace_html @ans, :partial => 'ans', :object => @ans
end
end

2 个答案:

答案 0 :(得分:0)

在您的单选按钮输入开始之前,您需要“f”,但乍一看您的控制器代码看起来非常低效:

 <%= f.radio_button :answer, "#{@ans.id}ans1" %> 

答案 1 :(得分:0)

实际上link_to_remote接下来是帖子并创建一个答案数据库,其中ans返回null .... 在我的开发日志处理AnswersController#next(2013-10-15 19:51:25为127.0.0.1)

[POST] Parameters: {"passed_question"=>"3", "action"=>"next", "authenticity_token"=>"EjReUuUVAbRJ6s3k7Tj/pS7s2ENUUgTvyaiiRz9/IF8=", "exam_group_id"=>"1", "controller"=>"answers"}

在这个我想要的答案参数,在单选按钮中给出以获得结果