所以,我在rails中遇到了一个问题,即在更新时并非所有参数都传递给控制器。这是has_many通过关系,我正在尝试更新连接表。
第一个模型如下:
class UserProfile < ApplicationRecord
belongs_to :user
belongs_to :profession
has_many :competencies, dependent: :destroy
has_many :procedure_categories, through: :profession
has_many :procedures, through: :competencies
accepts_nested_attributes_for :procedures
# required so that compentencies controller can save to the join table
accepts_nested_attributes_for :competencies
联接模式:
class Competency < ApplicationRecord
belongs_to :user_profile
belongs_to :procedure
end
我的表格:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<p>Define your competencies details here.</p>
<%= form_for @user_profile do |user_profile_form| %>
<% @user_profile.procedure_categories.each do |procedure_category| %>
<h2><%= procedure_category.category %></h2>
<%= user_profile_form.fields_for :competency do |cf| %>
<% competencies = @user_profile.competencies.where(procedure_id: procedure_category.procedures)
if competencies.count > 0
%>
<table class='table'>
<thead>
<tr>
<th>Competency</th>
<th>Years experience</th>
<th>Time it takes to complete</th>
<th>Do you want to do this?</th>
</tr>
</thead>
<tbody class="categories">
<% competencies.each do |competency| %>
<tr>
<td><%= competency.id %></td>
<td><%= cf.text_field :time, id: :competency_id, class: 'form-control' %></td>
<td><%= cf.text_field :rating, id: :competency_id, class: 'form-control' %></td>
<td>TBD</td>
</tr>
<% end %>
</tbody>
</table>
我的参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"onUmLRzPiJZmdSvd5PvG4zlskkn2I7FEfy7EgigcCHFFrC8/txI5a+wz6KTklFX80DGKNkZ/+7RYxPdCS8+nTA==", "user_profile"=>{"competency"=>{"time"=>"500", "rating"=>"500"}}, "commit"=>"Save competencies", "id"=>"11"}
当我检查HTML时,我看到了:
<tbody class="categories">
<tr>
<td>17</td>
<td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][time]" /></td>
<td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][rating]" /></td>
<td></td>
</tr>
<tr>
<td>19</td>
<td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][time]" /></td>
<td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][rating]" /></td>
<td></td>
</tr>
<tr>
<td>20</td>
<td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][time]" /></td>
<td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][rating]" /></td>
<td></td>
</tr>
</tbody>
因此,从生成的HTML中,我认为问题是我的所有参数看起来都一样。我需要以某种方式区分它们。
感谢您的帮助!
答案 0 :(得分:0)
所以,如果我看得太久,我会弄明白的。问题主要在于我的fields_for语句。以下是完成工作的所有表单代码:
$PSVersionTable