すべてのページでカレンダーを使用するために、Jquery UIのDatepicker関数をグローバルに使用しています。私は別々のJavaScriptファイルに上記の関数を呼び出すjavascript関数がjquery-ajaxのレール検証エラーで表示されない
<div class="field">
<%= f.label :Renewal_Date %>
<%= f.text_field :Renewal_Date, readonly: 'readonly', data: {field_type: date}}
</div>
、私はちょうど私の見解で私のDatePickerのフィールドを投稿
var showDatePickers = function() {
$('[data-field-type="date"]').datepicker({
dateFormat: "yy-mm--dd",
showOn: "both",
buttonImageOnly: true,
buttonImage: "/assets/blue-calendar-icon.png",
buttonText: "Calendar"
});
}
$(showDatePickers);
: 私は次のように別々のjavascriptのページを作成しました。
$(function() {
if ($('html.asset_contracts').length == 1) {
$(document.body).on('ajax:success', '.new_asset_contract, .edit_asset_contract', showDatePickers);
}
});
ページの読み込み、編集、および新しい操作がうまくいきました。しかし、レールの検証エラーが表示されてdatepicker関数が動作していないとき。それはブランクtext_field
を示しています。
FYI:これはajaxページで、new, create, update and edit
アクションはajax
ページとして機能しています。だから、私は自分のフォームでremote: true
を追加し、私はそれはあなたがそうのようなdatepickers作成している、
def create
@contract = Asset::Contract.new(params[:asset_contract])
respond_to do |format|
if @contract.save
format.html { redirect_to asset_contracts_path, notice: "Successfully Created" }
format.js
format.json { render json: @contract, status: :created, location: @contract }
else
format.html { render action: "new" }
format.js
format.json { render json: @contract.errors, status: :unprocessable_entity }
end
end
end
def update
@contract = Asset::Contract.find(params[:id])
respond_to do |format|
if @contract.update_attributes(params[:asset_contract])
format.html { redirect_to asset_contracts_path, notice: "Succesfully Updated" }
format.js
format.json { head :no_content }
else
format.html { render action: "edit" }
format.js
format.json { render json: @contract.errors, status: :unprocessable_entity }
end
end
end
おかげ
エラーが発生した場合、jsを確認してください。datetimeフィールドのセレクター '[data-field-type =" date "]' – yxf