Html 在Rails应用程序中选择多个单选按钮

Html 在Rails应用程序中选择多个单选按钮,html,ruby-on-rails,radio-button,Html,Ruby On Rails,Radio Button,我有一个应用程序,其中问题模型与选项有很多关系。我还有一个按钮,可以在创建问题时添加选项。每个问题只有一个正确答案。因此,当我创建一个问题并单击addoption按钮时,会创建新选项,但与之关联的新单选按钮具有不同的名称。实际上,单选按钮的名称的形式是问题[options\u attributes][i][is\u answer]其中i是id。据我所知,单选按钮应具有与集合或组相同的名称。那么,即使我为一个问题创建了任意数量的选项,我如何让它作为一个团队工作呢 html.erb <%= f

我有一个应用程序,其中
问题
模型
选项
有很多关系。我还有一个按钮,可以在创建问题时添加选项。每个问题只有一个正确答案。因此,当我创建一个问题并单击
addoption
按钮时,会创建新选项,但与之关联的新
单选按钮具有不同的
名称。实际上,
单选按钮的名称
的形式是
问题[options\u attributes][i][is\u answer]
其中
i
id
。据我所知,
单选按钮
应具有与集合或组相同的
名称。那么,即使我为一个问题创建了任意数量的选项,我如何让它作为一个团队工作呢

html.erb

<%= form_for @question do |form| %> 

  <div class="field">
    <%= form.label :body %>
    <%= form.text_area :body %>
  </div>

  <%= form.fields_for :options, question.options.each do |a| %>
    <div class="field">
      <%= a.label :options %>
      <%= a.text_area :body %>
      <%= a.radio_button :is_answer, "options" %>
      <%= a.check_box :_destroy %>
      <%= a.label :_destroy, 'delete' %>
    </div>    
  <% end %> 
 <%= form.submit 'Add option', :name => "add_option" %>
 <%= form.submit 'Delete options', :name => "remove_option" %>

  <div class="actions">
    <%= form.submit %>    
  </div>

<% end %>
class QuestionsController < ApplicationController
def new
    @question = Question.new
    @question.options.build
  end
def create
    @question = Question.new(question_params)
    @question.user = current_user
    if params[:add_option]
      @question.options.build
    else
      respond_to do |format|
        if @question.save
          format.html { redirect_to @question, notice: 'Question was successfully created.' and return }
          format.json { render :show, status: :created, location: @question }          
        else
          format.html { render :new }
          format.json { render json: @question.errors, status: :unprocessable_entity }
        end
      end
    end
    render :action => 'new'
  end
private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:body, options_attributes: [:id, :body, :question_id, :created_at, :updated_at, :is_answer])
    end
end

“添加选项”%>
“删除选项”%>
控制器.rb

<%= form_for @question do |form| %> 

  <div class="field">
    <%= form.label :body %>
    <%= form.text_area :body %>
  </div>

  <%= form.fields_for :options, question.options.each do |a| %>
    <div class="field">
      <%= a.label :options %>
      <%= a.text_area :body %>
      <%= a.radio_button :is_answer, "options" %>
      <%= a.check_box :_destroy %>
      <%= a.label :_destroy, 'delete' %>
    </div>    
  <% end %> 
 <%= form.submit 'Add option', :name => "add_option" %>
 <%= form.submit 'Delete options', :name => "remove_option" %>

  <div class="actions">
    <%= form.submit %>    
  </div>

<% end %>
class QuestionsController < ApplicationController
def new
    @question = Question.new
    @question.options.build
  end
def create
    @question = Question.new(question_params)
    @question.user = current_user
    if params[:add_option]
      @question.options.build
    else
      respond_to do |format|
        if @question.save
          format.html { redirect_to @question, notice: 'Question was successfully created.' and return }
          format.json { render :show, status: :created, location: @question }          
        else
          format.html { render :new }
          format.json { render json: @question.errors, status: :unprocessable_entity }
        end
      end
    end
    render :action => 'new'
  end
private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:body, options_attributes: [:id, :body, :question_id, :created_at, :updated_at, :is_answer])
    end
end
类问题控制器“新建”
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def集_问题
@question=question.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
定义问题参数
参数require(:question).permit(:body,options_属性:[:id,:body,:question_id,:created_at,:updated_at,:is_answer])
结束
结束
有两个选项:

  • 在客户端使用JavaScript取消选中单选按钮
  • 使用同名单选按钮。在这种情况下,您必须更改传递
    :is_answer
    参数的方式,并在
    选项\u属性中手动分配值
  • 方法1详细信息:

    看到这个问题了吗

    方法2详细信息:

    不是传递
    :每个选项的is_answer
    参数您可以为选择答案id作为值的问题传递单个参数。让我们把它命名为“答案id”
    。我们希望此参数位于
    params[question]
    在控制器中散列,因此整个名称将是
    “问题[答案id]”
    。尽管为每个选项生成了单选按钮,但只有所选的一个按钮将被发送到服务器,因为它们都具有相同的名称

        <%= form.fields_for :options, question.options.each do |a| %>
          <div class="field">
            <%= a.label :options %>
            <%= a.text_area :body %>
            <%= radio_button_tag "question[answer_id]", a.object.id, a.object.is_answer? %>
            <%= a.check_box :_destroy %>
            <%= a.label :_destroy, 'delete' %>
          </div>    
        <% end %>
    

    如果你需要更多的细节,请告诉我。我将更新答案以提供更多信息。

    添加了更多详细信息我尝试了第二种方法。我得到的
    未定义方法
    选项(u属性#
    问题
    对象如下:
    参数:{“utf8”=>”✓", "真实性令牌“=>”,“问题”=>{“身体”=>“俄罗斯的首都是哪里?”,“选项”=>{“0”=>{“身体”=>“莫斯科”,“破坏”=>“0”},“1”=>{“身体”=>“圣彼得堡”,“破坏”=>“0”},“回答”=>“在”},“提交”=>“创建问题”}
    更改为
    结果[:options\u属性]
    结果[:options\u属性].值
    另一个问题:所选单选按钮没有将
    is\u answer
    的值更改为
    true
    。我已将默认设置为
    false
    。因此,即使我选择了一个单选按钮,数据库中的值仍然是
    false