0

数据进入数据库,但它始终将 :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>
4

1 回答 1

0

所以我想通了。

隐藏的输入如何覆盖复选框。

所以我使用 check_box_tag 而不是 check_box,如下所示:

  <%= 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
于 2013-05-09T19:13:16.740 回答