Bootstrap 3.0 が提供するモーダルで送信されるフォームを作成しました。ただし、フォームは、ページがターボリンクされたときではなく、ページを更新したときにのみ送信されます。ターボリンクでは、送信ボタンをクリックし続けることができますが、何も起こりません。これは、何らかの理由で本番環境でのみ発生します。
ターボリンクを削除すると問題は解決しますが、それによる速度の向上が必要です。
考えられる問題の 1 つは、モーダルが最初は display:none; であるということです。そのため、フォームが正しく読み込まれていないことが問題である可能性があります。
モーダルなしでフォーム送信をテストしますが、作成中のアプリケーションには必須です。
deal_controller.rb
def create
@deal = Deal.new(deal_params)
@deal.team = @team
@deal.user_id = current_user.id
respond_to do |format|
if @deal.save
format.html { redirect_to team_deals_path(@team), notice: 'Deal was successfully created.' }
format.json { render action: 'show', status: :created, location: @deal }
else
@deals = @team.deals.all
iwantmodal
should_modal_be_open
format.html { render action: 'index' }
format.json { render json: @deal.errors, status: :unprocessable_entity }
end
end
end
index アクション自体にフォームがあります。
_form.html.erb
<div id="dealModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">New Deal</h4>
</div>
<div class="modal-body">
<%= simple_form_for([@deal.team, @deal]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :person_id do %>
<%= f.input_field :person_id, :collection => @team.people, class: "col-lg-10" %>
<%= link_to content_tag(:i, nil, class: 'glyphicon-plus'), new_team_person_path, type: 'button', class: 'btn btn-success col-lg-2' %>
<% end %>
<%= f.input :organization_id do %>
<%= f.input_field :organization_id, :collection => @team.organizations, class: "col-lg-10" %>
<%= link_to content_tag(:i, nil, class: 'glyphicon-plus'), new_team_organization_path, type: 'button', class: 'btn btn-success col-lg-2' %>
<% end %>
<%= f.input :title %>
<%= f.input :value %>
<%= f.input :currency_id, :include_blank => false , :collection => Currency.all, label_method: lambda { |obj| t("#{obj.extension}") } %>
<%= f.input :start_date, :as => :datetime_picker, input_html: {class: "deal_start_date"} %>
<%= f.input :close_date,:as => :datetime_picker, input_html: {class: "deal_close_date"} %>
<%= f.input :status, collection:['Pending', 'Won','Lost'], :include_blank => false %>
<%= f.input :phone %>
<%= f.input :visibility_id, collection: Visibility.all,:include_blank => false %>
<%= f.input :email %>
<%= f.input :stage_id, :collection => @team.stages, :include_blank => false %>
</div>
</div>
<div class="modal-footer">
<div class="form-actions">
<%= f.button :submit, class:"btn btn-primary", remote: true, method: :post %>
</div>
</div>
<% end %>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
if(<%= @modal %>) {
$('#dealModal').modal('show')
}
</script>
スクリプトは、フォームに検証エラーがある場合にモーダルを開いたままにするために使用されます。