[英]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 %>
show.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' %>
posts.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.