如何使用一个带有 rails 和 javascript 的提交按钮提交两个表单
How to submit two forms with one submit button with rails and javascript
我需要允许用户在要求他们提供帐户详细信息之前键入评论并提交。提供这些详细信息后,它们将用于创建新用户,并且他们编写的评论也会提交并属于该新用户。目前,我根本无法弄清楚如何在两个表单上触发提交操作,将所有信息传递给服务器以执行这些任务。
基本上我想要的:
- 用户写评论,提交
- "提交"按钮显示联系信息表单
- 用户填写此内容,提交
- 提交两种表格的数据
我有什么:
_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 %>
显示.html.erb
<% 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' %>
帖子.js
function contactDisplay(){
var contact = document.getElementById('contact');
contact.style.display='block';
}
function submitContact(){
document.getElementById("new_comment").submit();
document.getElementById("new_user").submit();
}
您可以使用嵌套表单:)这是关于如何做到这一点的轨道广播http://railscasts.com/episodes/196-nested-model-form-part-1
相关文章:
- Jquery提交表单而不刷新
- 如何在提交表单时将PHP变量传递到Javascript cookie中
- 每次提交表单时都会重新加载网页
- Razor中的自动提交表单
- 使用 JavaScript 自动提交表单
- 点击相同的按钮打开模型,然后提交表单
- 添加和删除隐藏字段数组中的值,而不提交表单
- 使用Ajax提交表单
- jQuery:使用“jQuery验证”后无法提交表单's远程验证
- 如何在不重新加载的情况下提交表单,并使用Node+Express将数据传递回同一页面
- 有登录表单时无法提交表单
- 带有select的jquery提交表单不起作用
- 如何防止回车键提交表单,但仍然允许回车工作
- 使用按钮和单选按钮提交表单,但始终使用相同的值
- 在提交表单之前确定重复值
- 如何创建动态ajax提交表单
- HTML提交表单,同时包含空字段检查和按钮隐藏
- 如何使用jQueryAjax使用动态html输入texbox提交表单
- 当触发下拉列表的onchange事件时,使用JavaScript提交表单
- 提交表单后,Watir文本框输入未持续(重置为以前的文本框输入)