form_for 在 rails 中不工作

form_for not working in rails

我的 form_for 单独使用时有效:

<h1>Contact Us</h1>
<%= @contact.errors.full_messages.join(', ') %>
<%= form_for @contact do |f| %>
  <p>
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </p>

  <p>
    <%= f.label :email %><br>
    <%= f.text_field :email %>
  </p>

  <p>
    <%= f.label :subject %><br>
    <%= f.text_field :subject %>
  </p>

  <p>
    <%= f.label :message %><br>
    <%= f.text_area :message %>
  </p>

  <p>
    <%= f.submit 'Submit Query', :class => 'submit-button btn btn-default' %>
  </p>
<% end %>

但是,当我将它添加到我的网络应用程序的其余部分时,它会停止工作:

<!-- scrollToTop -->
<!-- ================ -->
<div class="scrollToTop"><i class="icon-up-open-big"></i></div>

<!-- page wrapper start -->
<!-- ================ -->
<div class="page-wrapper">

  <!-- page-intro start-->
  <!-- ================ -->
  <div class="page-intro">
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <ol class="breadcrumb">
            <li><i class="fa fa-home pr-10"></i><a href="index.html">Home</a></li>
            <li class="active">Contact</li>
          </ol>
        </div>
      </div>
    </div>
  </div>
  <!-- page-intro end -->

  <!-- main-container start -->
  <!-- ================ -->
  <section class="main-container">

    <div class="container">
      <div class="row">

        <!-- main start -->
        <!-- ================ -->
        <div class="main col-md-12">

          <!-- page-title start -->
          <!-- ================ -->
          <h1 class="page-title">Contact Us</h1>
          <!-- page-title end -->
          <div class="row">
            <div class="col-md-6">
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do tempor.</p>
              <div class="alert alert-success hidden" id="MessageSent">
                We have received your message, we will contact you very soon.
              </div>
              <div class="alert alert-danger hidden" id="MessageNotSent">
                Oops! Something went wrong please refresh the page and try again.
              </div>
              <div class="contact-form">
                <form id="contact-form" role="form">
                  <%= @contact.errors.full_messages.join(', ') %>
                  <%= form_for @contact do |f| %>

                    <div class="form-group has-feedback">
                      <%= f.label :name %>
                      <%= f.text_field :name, :class => 'form-control', :id => 'name', :name => 'name' %>
                      <i class="fa fa-user form-control-feedback"></i>
                    </div>

                    <div class="form-group has-feedback">
                      <%= f.label :email %>
                      <%= f.text_field :email, :class => 'form-control', :id => 'email', :name => 'email' %>
                      <i class="fa fa-envelope form-control-feedback"></i>
                    </div>

                    <div class="form-group has-feedback">
                      <%= f.label :subject %>
                      <%= f.text_field :subject, :class => 'form-control', :id => 'subject', :name => 'subject' %>
                      <i class="fa fa-navicon form-control-feedback"></i>
                    </div>

                    <div class="form-group has-feedback">
                      <%= f.label :message %>
                      <%= f.text_area :message, :class => 'form-control', :rows => '6', :id => 'message', :name => 'message' %>
                      <i class="fa fa-pencil form-control-feedback"></i>
                    </div>

                    <%= f.submit 'Submit Query', :class => 'submit-button btn btn-default' %>

                  <% end %>
                </form>
              </div>
            </div>
            <div class="col-md-6">
              <!-- google maps start -->
              <div id="map-canvas"></div>
              <!-- google maps end -->
            </div>
          </div>
        </div>
        <!-- main end -->

      </div>
    </div>
  </section>
  <!-- main-container end -->

</div>
<!-- page-wrapper end -->

<!-- JavaScript files placed at the end of the document so the pages load faster
================================================== -->
<!-- Google Maps javascript -->
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"></script>

我正在使用 Action Mailer 发送电子邮件。它在第一个代码片段中发送带有独立表单的电子邮件。

您正在调用 form_for 辅助方法,在表单标记内。只需删除表单标签,只调用 form_for 辅助方法。它还生成表单标签。

希望,一切顺利。

您在表单标签内有 form_for 标签...

从 HTML 中删除 <form> 元素并将 form_for 标签改写为

<%= form_for @contact, id: 'contact-form' do |f| %>