数据进入数据库,但它始终将:location提交为0.如何避免这种情况?我希望:location提交选中的值。
另外,使用simple_form而不是像这样做会更容易吗?
以下是部分内容:
<%= form_for(@offering) do |f| %>
<%= render 'common/form_errors', object: @offering %>
<%= f.label :description, "Description:" %>
<%= f.text_field :description %>
<%= f.label :location, "Where?" %>
<%= check_box("offering", :location, {}, "Alphabet City") %>Alphabet City
<%= check_box("offering", :location, {}, "Battery Park") %>Battery Park
<%= check_box("offering", :location, {}, "Chelsea") %>Chelsea
<%= check_box("offering", :location, {}, "Chinatown") %>Chinatown
<%= f.submit "Post your offering" %>
<% end %>
以下是它产生的HTML:
<form accept-charset="UTF-8" action="/offerings" class="new_offering" id="new_offering" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"><input name="authenticity_token" type="hidden" value="L6oao6csQJ9TIRpEKG4BAzj3uH6FUMr9YmDYXx2HbFg="></div>
<label for="offering_description">Description:</label>
<input id="offering_description" name="offering[description]" size="30" type="text">
<label for="offering_location">Where?</label>
<input name="offering[location]" type="hidden" value="0">
<input id="offering_location" name="offering[location]" type="checkbox" value="Alphabet City">Alphabet City
<input name="offering[location]" type="hidden" value="0">
<input id="offering_location" name="offering[location]" type="checkbox" value="Battery Park">Battery Park
<input name="offering[location]" type="hidden" value="0">
<input id="offering_location" name="offering[location]" type="checkbox" value="Chelsea">Chelsea
<input name="offering[location]" type="hidden" value="0">
<input id="offering_location" name="offering[location]" type="checkbox" value="Chinatown">Chinatown
<input name="commit" type="submit" value="Post your offering">
</form>
答案 0 :(得分:0)
所以我想通了。
隐藏的输入一些如何超越复选框。
所以我没有使用check_box,而是使用check_box_tag:
<%= check_box_tag("offering[location]", "Alphabet City") %>Alphabet City
<%= check_box_tag("offering[location]", "Battery Park") %>Battery Park
<%= check_box_tag("offering[location]", "Chelsea") %>Chelsea
<%= check_box_tag("offering[location]", "Chinatown") %>Chinatown
而不是:
<%= check_box("offering", :location, {}, "Alphabet City") %>Alphabet City
<%= check_box("offering", :location, {}, "Battery Park") %>Battery Park
<%= check_box("offering", :location, {}, "Chelsea") %>Chelsea
<%= check_box("offering", :location, {}, "Chinatown") %>Chinatown