1

rails 3.2、twitter / bootstrap、formtasticを使用してモーダルに戻る検証を取得しようとしていますが、これを行う方法がわかりません。

以下は、私のモーダルとフォームがどのように見えるかの簡単なサンプルです。モデルでは、名と姓が必要です。ユーザーが名または姓を入力せずに「送信」を押すと、ユーザーはすぐに実際の登録ページに移動し、不足しているものを通知します。

しかし、私はそれをこのモーダルに戻し、何が欠けているかをユーザーに知らせたいと思います。どうすればこれを達成できますか?

:私のヘッダーでは、これが私がモーダルを立ち上げる方法です:

=link_to 'Register', "#new_registration_modal", :data => { :toggle => "modal",:backdrop => "static" }

#app / views /subscribers/registration/_new_modal.html.haml

#new_registration_modal.modal.hide.fade
  .modal-header
    Some Text
  .modal-body
    Some Text
    =semantic_form_for(:subscriber, :as => resource_name, :url => registration_path(resource_name),:html => { :class => "form-inline" } )  do |f|
      =f.input :email
      =f.input :firstname
      =f.input :lastname
      =f.input :password
      =f.input :password_confirmation
      =f.submit
  .modal-footer
    Some Text
4

1 に答える 1

4

私は(simple_formを使用して)私の側で機能する解決策を見つけました、多分あなたを助けることができます:

別のコントローラーからモーダル内に新しいクライアントを追加したい。

モーダルコントローラーのビュー:

<div id="client_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="client_modal_label" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="client_modal_label">Create new client</h3>
  </div>
  <div class="modal-body">
    <%= simple_form_for Client.new, html: {"data-type" => :json}, remote: true do |f| %>
    <div id="error_explanation" style="display:none;"></div>

    <div class="form-inputs">
      # the inputs
      # ... and "closing" the modal

client_controller.create:_

def create
 # save the data
 if @client.save
    respond_to do |format|
          format.html { redirect_to anagrafe_index_path, :notice => "Client create!"}
          format.json { render json: @client, status: :created, location: @client }
    end
 else
    respond_to do |format| 
        format.html { render :action => 'new'}
        format.json { render json: @client.errors.full_messages, status: :unprocessable_entity }
    end
 end
end

モーダルが発生するjs.coffee:

$ ()->
  $("form.new_client").on "ajax:success", (event, data, status, xhr) ->
    $("form.new_client")[0].reset()
    $('#client_modal').modal('hide')
    $('#error_explanation').hide()

  $("form.new_client").on "ajax:error", (event, xhr, status, error) ->
    errors = jQuery.parseJSON(xhr.responseText)
    errorcount = errors.length
    $('#error_explanation').empty()
    if errorcount > 1
      $('#error_explanation').append('<div class="alert alert-error">The form has ' + errorcount + ' errors.</div>')
    else
      $('#error_explanation').append('<div class="alert alert-error">The form has 1 error.</div>')
    $('#error_explanation').append('<ul>')
    for e in errors
      $('#error_explanation').append('<li>' + e + '</li>')
    $('#error_explanation').append('</ul>')
    $('#error_explanation').show()
于 2013-06-21T13:03:45.863 に答える