Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用rails和javascript通过一个提交按钮提交两个表单_Javascript_Ruby On Rails_Forms_Simple Form - Fatal编程技术网

如何使用rails和javascript通过一个提交按钮提交两个表单

如何使用rails和javascript通过一个提交按钮提交两个表单,javascript,ruby-on-rails,forms,simple-form,Javascript,Ruby On Rails,Forms,Simple Form,我需要允许用户在要求他们提供帐户详细信息之前键入评论并提交。一旦提供了这些详细信息,它们将用于创建新用户,并且它们编写的注释也将提交并属于该新用户。目前,我根本不知道如何在两个表单上触发提交操作,将所有信息传递给服务器以执行这些任务 基本上是我想要的: 用户撰写评论,提交 “提交”按钮显示联系人信息表单 用户填写并提交 提交两份表格中的数据 我所拥有的: _feedback.html.erb <%= simple_form_for [@post, Comment.new] do |f|

我需要允许用户在要求他们提供帐户详细信息之前键入评论并提交。一旦提供了这些详细信息,它们将用于创建新用户,并且它们编写的注释也将提交并属于该新用户。目前,我根本不知道如何在两个表单上触发提交操作,将所有信息传递给服务器以执行这些任务

基本上是我想要的:

用户撰写评论,提交 “提交”按钮显示联系人信息表单 用户填写并提交 提交两份表格中的数据 我所拥有的:

_feedback.html.erb

  <%= simple_form_for [@post, Comment.new] do |f| %>
      <%= f.input :body, :label => false, :input_html => { :maxlength => '500', :rows => '4', :placeholder => '...'}  %>
      <% if current_user.guest != true %>
        <%= f.button :submit, "Submit" %>
      <% end %>
  <% end %>
  <% if current_user.guest == true %>
    <input type="submit" value="Submit" onclick="contactDisplay()"></input>
  <% end %>

您可以使用嵌套表单:下面是一个railscasts如何实现的示例

你不需要两张表格,为什么不用一张表格呢?你不能同时提交两张表格。请看@Ibu,它们是不同的型号。@Vaheh我想这是不可能的。我的问题是怎么做。我很感激这个链接,但它并没有太大的帮助,因为我对ajax不是很了解。在这个问题上,你将不得不使用ajax。如果你把它和你的朋友一起使用,那就很容易了
<% if current_user.guest == true %>
  <div id="contact" class="post-contact">
    <%= simple_form_for(@user, :html => { :multipart => true }, defaults: { label: false, required: true }) do |f| %>
      <%= f.input :name, :input_html => { :placeholder => 'Your Name', :id => 'name'} %>
      <%= f.input :email, :input_html => { :placeholder => 'Your Email', :id => 'email'} %>
    <% end %>
    <input type="submit" value="Submit" onclick="submitContact()"></input>
  </div>
<% end %>
<%= render 'posts/show/feedback' %>
function contactDisplay(){
var contact = document.getElementById('contact'); 
    contact.style.display='block';
}

function submitContact(){
    document.getElementById("new_comment").submit();
    document.getElementById("new_user").submit();
}