Rails中的嵌套表单模型不保存使用jQuery添加的项目

时间:2013-04-19 21:15:09

标签: ruby-on-rails ruby-on-rails-3

我正在关注Ryan Bate的RailsCast(#197)的嵌套模型表格。

如果我提交的表单在控制器中添加了3个字段:

3.times { @prof.experiences.build }

......他们保存得当。但是,如果通过jQuery添加该字段,则不会保存该字段。

以前有其他人有这种问题的经验吗?

编辑:HTML生成没有jQuery:

<div class="fields">
<table>
<tbody><tr>
<td>Position Title:</td>
<td><input id="applicant_profile_experiences_attributes_0_title" name="applicant_profile[experiences_attributes][0][title]" size="30" type="text"></td>
</tr>
<tr>
<td>Employer:</td>
<td><input id="applicant_profile_experiences_attributes_0_employer" name="applicant_profile[experiences_attributes][0][employer]" size="30" type="text"></td>
</tr>
<tr>
<td>Responsibilities</td>
<td><textarea cols="40" id="applicant_profile_experiences_attributes_0_description" name="applicant_profile[experiences_attributes][0][description]" rows="4"></textarea></td>
</tr>
<tr>
<td>Start Date:</td>
<td><select id="experience_from_date_1i" name="experience[from_date(1i)]">
<option value="1970">1970</option>
<option value="1971">1971</option>
</select>
</td>
</tr>

</tbody></table>
<p><input id="applicant_profile_experiences_attributes_0__destroy" name="applicant_profile[experiences_attributes][0][_destroy]" type="hidden" value="false"><a href="#" onclick="remove_fields(this); return false;">Remove</a></p>
</div>

使用jQuery添加代码:

<div class="fields">
<table>
    <tbody><tr>
        <td>Position Title:</td>
        <td><input id="applicant_profile_experiences_attributes_1366407307283_title" name="applicant_profile[experiences_attributes][1366407307283][title]" size="30" type="text"></td>
    </tr>
    <tr>
        <td>Employer:</td>
        <td><input id="applicant_profile_experiences_attributes_1366407307283_employer" name="applicant_profile[experiences_attributes][1366407307283][employer]" size="30" type="text"></td>
    </tr>
    <tr>
        <td>Responsibilities</td>
        <td><textarea cols="40" id="applicant_profile_experiences_attributes_1366407307283_description" name="applicant_profile[experiences_attributes][1366407307283][description]" rows="4"></textarea></td>
    </tr>
    <tr>
        <td>Start Date:</td>
        <td><select id="experience_from_date_1i" name="experience[from_date(1i)]">
<option value="1970">1970</option>
<option value="1971">1971</option>
<!-- removed big date list -->
</select>
</td>
    </tr>

</tbody></table>
<p><input id="applicant_profile_experiences_attributes_1366407307283__destroy" name="applicant_profile[experiences_attributes][1366407307283][_destroy]" type="hidden" value="false"><a href="#" onclick="remove_fields(this); return false;">Remove</a></p>
</div>

0 个答案:

没有答案