Home>

When creating a post form in root_url and passing information with form_for
When I update, submit will respond
The first time does not respond.
I have a similar question, but if you haven't solved it and want to break through the same situation, I'd like to see a professor.

<div>
<% = form_for (@micropost, html: {class: 'form-horizontal'}) do | f |%>
<% = render 'shared/error_messages', object: f.object%>
    <table>
        <tbody>
            <tr>
               <th>Product Name</th>
               <th>Image</th>
            </tr>
                <td><% = f.text_field: product, placeholder: "Please enter product name"%></td>
                <td><% = f.file_field: image, accept: 'image/jpeg, image/gif, image/png'%></td>
        </tbody>
    </table>

</div>
<div>
  <table>
          <tbody>
            <tr>
               <th>Location</th>
               <th>Time</th>
               <th>More info</th>
               <th>Action</th>
            </tr>
            <% = f.nested_fields_for: blocks, wrapper_tag:: tr do | q |%>
                <td><% = q.select: place, Block.places.keys, {},
                                     class: 'form-control'%></td>
                <td><% = q.date_select: date, class: 'form-control'%></td>
                <td><% = q.text_area: information, placeholder: "Please enter",
                                      class: 'form-control'%>
                    <% = q.file_field: picture, accept: 'image/jpeg, image/gif, image/png'%>
                </td>
                <td><% = q.remove_nested_fields_link 'delete', class: 'btn btn-danger', role: 'button'%></td>
            <% end%>
        </tbody>
      </table>
      <% = f.add_nested_fields_link: blocks, 'add info', class: 'btn btn-primary add-block-btn', role: 'button'%><br>
</div>
</div>
<% = f.submit "Create", class: "btn btn-primary submit"%>
<% end%>

<script type = "text/javascript">
  $('# micropost_image'). bind ('change', function () {
    var size_in_megabytes = this.files [0] .size/1024/1024;
    if (size_in_megabytes>5) {
      alert ('Maximum file size is 5MB. Please choose a smaller file.');
    }
  });
</script>
  • Answer # 1

    Self-solved.
    I don't know the cause, but it seems that the nest structure was complicated and wrong.
    Bugs disappeared when the form was greatly improved.

Related articles