什么是html表单的正确erb等价物?

时间:2018-06-14 04:47:31

标签: html ruby-on-rails forms erb

我有一个表单的html代码。除了我想使用erb form_for 来创建新联系人之外,我希望保持其外观。表单应由 Contacts Controller 处理,定义的实例变量名为 @contact 。什么是正确的erb保持完全相同的外观,而是使用

 <% form_for @contact do |f| %>
        #code
  <% end %>

以下是原始的html表单。提前谢谢。

<div class="contact_from">
                    <form action="#" method="post">
                        <!-- Message Input Area Start -->
                        <div class="contact_input_area">
                            <div class="row">
                                <!-- Single Input Area Start -->
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <input type="text" class="form-control" name="name" id="name" placeholder="Your Name" required>
                                    </div>
                                </div>
                                <!-- Single Input Area Start -->
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <input type="email" class="form-control" name="email" id="email" placeholder="Your E-mail" required>
                                    </div>
                                </div>
                                <!-- Single Input Area Start -->
                                <div class="col-12">
                                    <div class="form-group">
                                        <textarea name="message" class="form-control" id="message" cols="30" rows="4" placeholder="Your Message *" required></textarea>
                                    </div>
                                </div>
                                <!-- Single Input Area Start -->
                                <div class="col-12">
                                    <button type="submit" class="btn submit-btn">Send Now</button>
                                </div>
                            </div>
                        </div>
                        <!-- Message Input Area End -->
                    </form>
                </div>

2 个答案:

答案 0 :(得分:1)

使用以下活动视图表单帮助程序:

<%= form_for @contact do |f| %>
<% end %>

并替换 <input type="text" class="form-control" name="name" id="name" placeholder="Your Name" required>具有以下活动视图助手

<%= f.text_field :name, required: true, class: 'form-control' %>

为其他输入标签做类似的事情。

您可以按原样保留所有div

阅读this doc以便更好地理解。

答案 1 :(得分:0)

您将div放在rails form_for方法中。并使用帮助程序创建输入字段。

<% form_for @contact, :url => {:action => "#"}, method: 'post' do |f| %>
<div class="contact_from">
  <!-- Message Input Area Start -->
  <div class="contact_input_area">
    <div class="row">
      <!-- Single Input Area Start -->
      <div class="col-md-12">
        <div class="form-group">
          <%= f.text_field :name, required: true, class: 'form-control', placeholder: "Your Name" %>
        </div>
      </div>
      <!-- Single Input Area Start -->
      <div class="col-md-12">
        <div class="form-group">
          <%= f.text_field :email, required: true, class: 'form-control', placeholder: "Your E-mail" %>
        </div>
      </div>
      <!-- Single Input Area Start -->
      <div class="col-12">
        <div class="form-group">
          <%= f.text_area :message, required: true, :size => "30x4", class: 'form-control', placeholder: 'Your Message *' %>
        </div>
      </div>
      <!-- Single Input Area Start -->
      <div class="col-12">
        <button type="submit" class="btn submit-btn">Send Now</button>
      </div>
    </div>
  </div>
  <!-- Message Input Area End -->
</div>
<% end %>