0

我有一个这样构建的选择标签:

<%= f.select(:id, category_options, include_blank:true) %>

表单显示正常,并选择了适当的模型项的选项标签。但是,当用户选择不同的选项并提交表单时,新选项不会传递给服务器。相反,最初选择的值被传递给服务器。

rails 日志显示正在提交的旧值,而不是新值。此输入的值似乎已缓存。这发生在开发和生产中,并且在开发中 cache_classes = false。

rails 日志如下:

在 2012-09-11 13:42:51 -0400 开始为 127.0.0.1 PUT "/posts/1999" 由 GalleriesController#update 作为 HTML 处理

参数:{"utf8"=>"✓", "authenticity_token"=>"...", "gallery"=>{"publish_on"=>"2012-09-18", "poster_file_temp"=>"", "thumb_file_temp"=>"", "models_attributes"=>{"0"=>{"id"=>"116", "_destroy"=>"false"}, "1"=>​​{"id"=> "6409", "_destroy"=>"false"}}, "categories_attributes"=>{"0"=>{"id"=>"59", "_destroy"=>"false"}}, "thumb_zip_input" =>"", "medium_zip_input"=>"", "large_zip_input"=>"", "website_url"=>"", "commit"=>"上传", "id"=>"1999"}

表格如下所示:

        <form accept-charset="UTF-8" action="/galleries/1999" class="edit_gallery" enctype="multipart/form-data" id="edit_gallery_1999" method="post" name="edit_gallery_1999">
        <div style="margin:0;padding:0;display:inline">
            <input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="put"><input name="authenticity_token" type="hidden" value="...">
        </div>
        <div class="vert">
            <label for="gallery_publish_on">Publish on</label> <input id="gallery_publish_on" name="gallery[publish_on]" size="30" type="text" value="2012-09-18"> <label for="gallery_poster_file">Poster file</label> <input id="gallery_poster_file_temp" name="gallery[poster_file_temp]" type="hidden"><input id="gallery_poster_file" name="gallery[poster_file]" size="30" type="file">
            <div class='formSection'>
                <img alt="Brandy" src="/content/gallery/poster_file/0000/1999/brandy.jpg">
            </div><label for="gallery_thumb_file">Thumb file</label> <input id="gallery_thumb_file_temp" name="gallery[thumb_file_temp]" type="hidden"><input id="gallery_thumb_file" name="gallery[thumb_file]" size="30" type="file">
            <div class='formSection'>
                <img alt="Thumb" src="/content/gallery/thumb_file/0000/1999/thumb.jpg">
            </div>
            <div id="models">
                <label for="Models">Models</label>
                <div>
                    <select id="gallery_models_attributes_0_id" name="gallery[models_attributes][0][id]">
                        <option value="6409">
                            April
                        </option>
                        <option value="6366" selected="selected">
                            Brandy
                        </option>
                        <option value="9036">
                            Charlie
                        </option>
                        <option value="7490">
                            Craig
                        </option>
                        <option value="9881">
                            Donna
                        </option>
                    </select> <input id="gallery_models_attributes_0__destroy" name="gallery[models_attributes][0][_destroy]" type="hidden" value="false"> <a href="#" class="remove_fields">remove</a>
                </div><input id="gallery_models_attributes_0_id" name="gallery[models_attributes][0][id]" type="hidden" value="6366"> <a href="#" class="add_fields" data-fields="&lt;div&gt; &lt;select id=&quot;gallery_models_attributes_70106238269060_id&quot; name=&quot;gallery[models_attributes][70106238269060][id]&quot;&gt;&lt;option value=&quot;&quot;&gt;&lt;/option&gt;&lt;option value=&quot;6409&quot;&gt;April&lt;/option&gt;&lt;option value=&quot;6366&quot;&gt;Brandy&lt;/option&gt;&lt;option value=&quot;9036&quot;&gt;Charlie&lt;/option&gt;&lt;option value=&quot;7490&quot;&gt;Craig&lt;/option&gt;&lt;option value=&quot;9881&quot;&gt;Donna&lt;/option&gt;&lt;/select&gt; &lt;input id=&quot;gallery_models_attributes_70106238269060__destroy&quot; name=&quot;gallery[models_attributes][70106238269060][_destroy]&quot; type=&quot;hidden&quot; value=&quot;false&quot; /&gt; &lt;a href=&quot;#&quot; class=&quot;remove_fields&quot;&gt;remove&lt;/a&gt;&lt;/div&gt;" data-id="70106238269060">add model</a>
            </div>
            <div id="categories">
                <label for="Categories">Categories</label>
                <div>
                    <select id="gallery_categories_attributes_0_id" name="gallery[categories_attributes][0][id]">
                        <option value="1">
                            Amateur
                        </option>
                        <option value="2">
                            Animals
                        </option>
                        <option value="49">
                            Articles
                        </option>
                        <option value="3">
                            Art
                        </option>
                        <option value="7">
                            Blog
                        </option>
                    </select> <input id="gallery_categories_attributes_0__destroy" name="gallery[categories_attributes][0][_destroy]" type="hidden" value="false"> <a href="#" class="remove_fields">remove</a>
                </div><input id="gallery_categories_attributes_0_id" name="gallery[categories_attributes][0][id]" type="hidden" value="59"> <a href="#" class="add_fields" data-fields="&lt;div&gt; &lt;select id=&quot;gallery_categories_attributes_70106278052860_id&quot; name=&quot;gallery[categories_attributes][70106278052860][id]&quot;&gt;&lt;option value=&quot;&quot;&gt;&lt;/option&gt;&lt;option value=&quot;1&quot;&gt;Amateur&lt;/option&gt;&lt;option value=&quot;2&quot;&gt;Animal&lt;/option&gt;&lt;option value=&quot;49&quot;&gt;Articles&lt;/option&gt;&lt;option value=&quot;3&quot;&gt;Art&lt;/option&gt;&lt;option value=&quot;7&quot;&gt;Blog&lt;/option&gt;&lt;/select&gt; &lt;input id=&quot;gallery_categories_attributes_70106278052860__destroy&quot; name=&quot;gallery[categories_attributes][70106278052860][_destroy]&quot; type=&quot;hidden&quot; value=&quot;false&quot; /&gt; &lt;a href=&quot;#&quot; class=&quot;remove_fields&quot;&gt;remove&lt;/a&gt;&lt;/div&gt;" data-id="70106278052860">add category</a>
            </div>
            <div id='photoset_files'>
                <fieldset>
                    <legend>Zip Files</legend> <label for="gallery_thumb_zip_input">thumb zip</label> <select id="gallery_thumb_zip_input" name="gallery[thumb_zip_input]">
                        <option value="/Library/WebServer/Documents/uploads/thumb copy.zip">
                            thumb copy.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/thumb.zip">
                            thumb.zip
                        </option>
                    </select>&nbsp;[ thumb.zip ]<br>
                    <label for="gallery_medium_zip_input">medium zip</label> <select id="gallery_medium_zip_input" name="gallery[medium_zip_input]">
                        <option value="/Library/WebServer/Documents/uploads/medium copy 10.zip">
                            medium copy 10.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/medium copy 9.zip">
                            medium copy 9.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/medium copy.zip">
                            medium copy.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/medium.zip">
                            medium.zip
                        </option>
                    </select>&nbsp;[ medium.zip ]<br>
                    <label for="gallery_large_zip_input">large zip</label> <select id="gallery_large_zip_input" name="gallery[large_zip_input]">
                        <option value="/Library/WebServer/Documents/uploads/large copy 10.zip">
                            large copy 10.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/large copy 9.zip">
                            large copy 9.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/large copy.zip">
                            large copy.zip
                        </option>
                        <option value="/Library/WebServer/Documents/uploads/large.zip">
                            large.zip
                        </option>
                    </select>&nbsp;[ large.zip ]<br>
                </fieldset>
            </div><label for="gallery_website_url">Website url</label> <input class="wider" id="gallery_website_url" name="gallery[website_url]" size="30" type="text" value=""> <input id="tluo" name="tluo" type="hidden" value="aHR0cDovL2tmLmRldi9nYWxsZXJpZXMvbGlzdA==">
            <div id="submit">
                <input data-disable-with="Uploading..." name="commit" type="submit" value="Upload"> &nbsp;&nbsp; <a href="/galleries/">Cancel</a>
            </div>
        </div>
    </form>
4

1 回答 1

1

这是因为您正在重复 select 的 id 和 name 属性。不建议有两个具有相同 id 值的 html 元素

尝试更改选择的名称,或者如果您想将其用作数组,那么以这种方式指定名称可能会对您有所帮助

 gallery[categories_attributes][0][id][]

这个我用来收集选中的单选按钮值,它可能适用于你的情况

于 2012-09-11T19:04:00.727 回答