首页 > 解决方案 > 如何修复 Rails 单选按钮集合总是返回最后一个值?

问题描述

我们有一个包含许多模型的应用程序,其中两个表现得很奇怪(Candidate 和 MarketSegment)。

Candidate 类的每个实体都拥有一个 market_segment_id 属性。我们还有一个表单,允许我们编辑 Candidate 实例并通过 JSON 请求提交/更新。

当我尝试渲染一个构建单选按钮集合的部分时,该单选按钮的集合具有要在编辑表单中为该候选人选择的市场细分选项,我总是在 params 哈希中获取单选按钮列表的最后一个 value/market_segment_id,即market_segment_id => '35' 每次。

我们已经尝试了许多不同的方法,包括:

1) 检查 html 以确认 'collection_radio_buttons' 助手是否为每个按钮生成正确的值;

2)构建循环以手动创建具有各自属性的每个单选按钮。

<%= render partial: 'shared/forms/market_segments_select', locals: {f: f} %>

生成单选按钮列表的 ruby​​/erb 部分内容

<div class="w-100 h4 overflow-scroll overflow-x-hidden bn pt3">
  <%= f.collection_radio_buttons :market_segment_id, MarketSegment.all, :id, :description_pt do |m| %>
    <div class="w-100 mb2 market_segment__radio-group">
      <%= m.radio_button %>
      <%= m.label %>
    </div>
  <% end %>
</div>

部分生成的html

<div class="w-100 h4 overflow-scroll overflow-x-hidden bn pt3">
  <input type="hidden" name="candidate[market_segment_id]" value="" />
    <div class="w-100 mb2 market_segment__radio-group">
      <input type="radio" value="1" name="candidate[market_segment_id]" id="candidate_market_segment_id_1" />
      <label for="candidate_market_segment_id_1">Agronegócio e Bioenergia</label>
    </div>

    <div class="w-100 mb2 market_segment__radio-group">
      <input type="radio" value="2" name="candidate[market_segment_id]" id="candidate_market_segment_id_2" />
      <label for="candidate_market_segment_id_2">Alimentos</label>
    </div>

    <div class="w-100 mb2 market_segment__radio-group">
      <input type="radio" value="3" name="candidate[market_segment_id]" id="candidate_market_segment_id_3" />
      <label for="candidate_market_segment_id_3">Auditoria</label>
    </div>
...and so on until market_segment_id = 35

参数

Parameters: {"utf8"=>"✓", "authenticity_token"=>"Sgpo39Lo...==", "candidate"=>{"avatar"=>"", ..., "market_segment_id"=>"35", ...}, "commit"=>"Atualizar", "id"=>"8"}

控制器动作

def update
    respond_to do |format|
      if @candidate.update(candidate_params)
        create_candidate_skill_tags

        format.html do
          redirect_to admin_candidate_path(@candidate),
                      notice: 'Candidato atualizado com sucesso'
        end
        format.json { render json: @candidate, status: :ok }
      else
        format.html { render :edit }
        format.json do
          render json: @candidate.errors, status: :unprocessable_entity
        end
     end
  end
end

无论单选按钮如何,我选择的参数总是得到 market_segment_id => '35' (这是 MarketSegment 表的最后一条记录。

标签: ruby-on-railsruby

解决方案


我做过类似的事情。你可以做这样的事情

  def create_candidate_skill_tags 
     new_skill_tags_from_params.map do |skill_text| 
      @candidate.skill_tags << SkillTag.where(id: 
    skill_text).first_or_create
  end
 end


<label for="users"><strong>Your Form</strong></label>
<div class="custom-select form-control pull-right">
 <%= form.collection_check_boxes(:market_segment_id, MarketSegment.all, :id, 
    :description, 
  checked: @candidate.market_segments.map(&:id)) do |b| %>    
    <%= b.check_box %> <%= b.label %>
 <% end %>
</div>

你也需要你的candidate.rb

has_and_belongs_to_many :market_segments, foreign_key: :candidate_id

在 market_segment.rb 中

has_and_belongs_to_many :candidates 

确保您:candidate_id的数据库中有列。或者你是怎么做的。


推荐阅读