2016-07-05 11 views
0

私はオフィスのリストを持つウェブページを持っています。現在、私はオフィスを編集/追加/削除するための3つのフォームを作ろうとしています。これは私が持っているものです。1つのオブジェクトの3つのフォームがモーダルになります

モデル:私はパーシャルで私のモーダルを持つビューで

class ChangeOfficeAddress < ApplicationRecord 
    belongs_to :office 
    belongs_to :insurer 
    belongs_to :city 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates_presence_of :email 
    validates_format_of :email, with: VALID_EMAIL_REGEX 
    validates_presence_of :edit_office_address 
    validates_presence_of :add_office_address 
    validates_presence_of :delete_office_address 
    validates_presence_of :city_id 
    validates_presence_of :insurer_id 
    validates_presence_of :name 
end 

<div id="addModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true"> 
    <div class="modal-dialog"> 

    <!-- Modal content--> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <div class="text-center"> 
      <div class="btn-group topbar header-buttons" role="group" aria-label="..."> 
      <%= link_to 'Add', '#', class: 'btn btn-default disabled' %> 
      <%= link_to 'Edit', '#editModal', { 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-dismiss' => 'modal' } %> 
      <%= link_to 'Delete', '#deleteModal', { 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-dismiss' => 'modal' } %> 
      </div> 
     </div> 
     </div> 
     <div class="modal-body"> 
     <%= form_for (@change_office_address), remote: true, format: :json, html: { class: :contact_form } do |f| %> 
      <div id="error_explanation" style='display:none;' class="bg-danger text-danger alert fade in alert-danger alert-dismissable errors"> 
      <ul> 
       <% if @change_office_address.errors.any? %> 
       <% @change_office_address.errors.full_messages.each do |msg| %> 
        <li><%= msg %></li> 
       <% end %> 
       <% end %> 
      </ul> 
      </div> 
      <%= f.text_field :name, placeholder: 'Name', class: 'form-control' %> 

      <br> 
      <%= f.text_field :email, placeholder: 'e-mail', class: 'form-control' %> <br> 
      <%= f.label :city_id %> 
      <%= f.collection_select :city_id, City.order(:name), :id, :name, 
            { include_blank: true }, { class: 'form-control' } %> 
      <br> 
      <%= f.label :insurer_id, 'Insurer' %> 
      <%= f.collection_select :insurer_id, Insurer.order(:short_name), :id, :short_name, 
            { include_blank: true }, { class: 'form-control' } %> 
      <br> 
      <%= f.text_area :add_office_address, placeholder: 'Add address', class: 'form-control', cols: '30', 
          rows: '5' %> <br> 
      <div class="text-center"> 
      <%= f.submit, class: 'btn btn-default' %> 
      </div> 
     <% end %> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     </div> 
    </div> 

    </div> 
</div> 

そして他の二つのモーダル、それらの間の唯一の違いはさその:add_office_address:edit_office_address:delete_office_addressに置き換えられます。

モデルには検証がない場合は、フォームを提出し、すべてがOKですが、私は:add_office_address:edit_office_address:delete_office_addressに検証を追加するとき、検証が通らない、これらのフィールドを引き起こす(私は意味:edit_office_address:delete_office_addressまたは:add_office_address)は空白です。

さまざまなフォームを作成するにはどうすればよいですか?先にありがとう!

答えて

1

状況によっては、[add|edit|delete] _office_addressのフィールドが異なる理由は何ですか?

class ChangeOfficeAddress < ApplicationRecord 
    belongs_to :office 
    belongs_to :insurer 
    belongs_to :city 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates_presence_of :email 
    validates_format_of :email, with: VALID_EMAIL_REGEX 
    validates_presence_of :edit_office_address, if: :edit_office_address_changed? 
    validates_presence_of :add_office_address, if: :add_office_address_changed? 
    validates_presence_of :delete_office_address, if: :delete_office_address_changed? 
    validates_presence_of :city_id 
    validates_presence_of :insurer_id 
    validates_presence_of :name 
end 

は、それが動作するかどうか、私に教えてください:

はしかし、あなたの問題を解決するために、あなたはおそらくのような何かを行う必要があります。

+0

ええ、ありがとう!完璧 –

関連する問題