2015-08-20 8 views
0

У меня есть проблема с этим:Две формы, один представить рельсы

Моя первая форма

<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" 

Мне нужно это, чтобы сохранить правду и не осознавать этого. Я делаю что-то неправильно, пожалуйста, помогите.

С уважением.

+0

Посмотрите http://stackoverflow.com/questions/19260755/how-to- submit-two-forms-with-one-submit-button-with-rails-and-javascript – Arvind

+0

В чем проблема, с которой вы сталкиваетесь? Что происходит? –

ответ

0

Рекомендуется использовать только одну форму. Но если вы хотите использовать несколько форм в рельсах, вы можете использовать семантическую форму для семантических вложенных полей, которые помогут вам использовать несколько форм, а также полезны для сохранения данных с отношением. 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 
Смежные вопросы