两份表格,一份提交 rails

Two forms, one submit rails

我对此有疑问:

我的第一个表格

 <div class="authform">
   <h3>Edit <%= resource_name.to_s.humanize %></h3>
   <%= form_for(current_user, :url => registration_path(current_user ), :html => { :method => :put, :role => 'form', :id => "form1"}) do |f| %>
     <%= devise_error_messages! %>
     <div class="form-group">
       <%= f.label :nombre %>
       <%= f.text_field :nombre, :autofocus => true, class: 'form-control' %>
     </div>
      <div class="form-group">
       <%= f.label :email %>
       <%= f.email_field :email, class: 'form-control' %>
      </div>
     </div>
      <% end %>
 </div>

第二种形式是

  <%= simple_form_for(@auto, html: { method: :post, id: :subir }) do |f| %>
    <%= f.error_notification %>

    <div class="form-inputs">
       <%= f.association :region %>
       <%= f.association :ciudad %>
       <%= f.association :marca %>
       <%= f.input :modelo %>
       <%= f.input :version %>
   </div>
  <% end %>

  <%= link_to "Save", edit_user_registration_path, :class => 'button_submit' %>

auto.coffee

 jQuery ->
    $(".button_submit").live "click", (e) ->
      e.preventDefault()
      $("#form1").trigger "submit"
      $("#subir").trigger "submit"

我需要这个来保持真相,但没有意识到这一点。我做错了什么,请帮忙。

此致。

建议只使用一种形式。但是如果你想在 rails 中使用多种形式,你可以使用语义形式和语义嵌套字段,这将帮助你使用多种形式,也有助于保存关系数据。 https://github.com/ryanb/nested_form供大家参考。

修改成这样,

<%= semantic_form_for current_user, :url => registration_path(current_user ), :html => { :method => :put, :role => 'form', :id => "form1"} do |f| %>
  <%= devise_error_messages! %>
  <div class="form-group">
    <%= f.label :nombre %>
    <%= f.text_field :nombre, :autofocus => true, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :email %>
    <%= f.email_field :email, class: 'form-control' %>
  </div>
  <%= f.semantic_fields_for(@auto, html: { method: :post, id: :subir }) do |form| %>
    <%= form.error_notification %>
    <div class="form-inputs">
      <%= form.association :region %>
      <%= form.association :ciudad %>
      <%= form.association :marca %>
      <%= form.input :modelo %>
      <%= form.input :version %>
    </div>
  <% end %>
<% end %>

你也应该在用户模型中,

 class User < ActiveRecord::Base
    has_many :posts
    accepts_nested_attributes_for :posts, :reject_if => :all_blank, :update_only => true, :allow_destroy => true
  end