为什么在提交表单时没有将新选择的选项发送到服务器?

时间:2012-09-11 18:24:30

标签: ruby-on-rails-3 forms select option

我有一个像这样构建的select标签:

<%= 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”=&gt;“✓”,“authenticity_token”=&gt;“...”,“gallery”=&gt; {“publish_on”=&gt;“2012-09-18”,“ poster_file_temp“=&gt;”“,”thumb_file_temp“=&gt;”“,”models_attributes“=&gt; {”0“=&gt; {”id“=&gt;”116“,”_ destroy“=&gt;”false“ },“1”=&gt; {“id”=&gt;“6409”,“_ destroy”=&gt;“false”}},“categories_attributes”=&gt; {“0”=&gt; {“id”=&gt; ;“59”,“_ destroy”=&gt;“false”}},“thumb_zip_input”=&gt;“”,“medium_zip_input”=&gt;“”,“large_zip_input”=&gt;“”,“website_url”=&gt; “”,“commit”=&gt;“上传”,“id”=&gt;“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>

1 个答案:

答案 0 :(得分:1)

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

尝试更改select的名称,或者如果要将其用作数组,则以这种方式指定名称可能会帮助您

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

这个我用来收集已检查的单选按钮值,它可以在你的情况下工作