[英]Rendering Error Messages in Rails Form with Javascript
我有一個應該呈現錯誤消息的表單,但是我不確定如何呈現錯誤消息,因為單擊按鈕后,我的表單正在使用Javascript顯示。 我正在使用Devise進行注冊,但正在運行,只是不顯示錯誤消息。 我正在使用Rails 4。
這是單擊后隱藏注冊按鈕並顯示表單的代碼:
<script type="text/javascript">
$(function() {
$('#prelaunchbutton').click(function() {
$('#prelaunchregistrationbutton').fadeToggle('slow');
$('.prelaunchhide').fadeToggle('slow');
return false;
});
});
</script>
這是Devise注冊表格(為簡便起見,以下簡稱):
<div class="prelaunch_preregisterbutton" id="prelaunchregistrationbutton">
<%= link_to image_tag("pre-register.png", :border=>0), '#', id:'prelaunchbutton' %>
</div>
<div class="span10 offset1 prelaunchhide">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :class => "form-inline") do |f| %>
...
<% end %>
</div>
這是Devise注冊控制器:
class RegistrationsController < Devise::RegistrationsController
before_filter :authenticate_user!
respond_to :html, :json
def new
end
def create
@user = User.new(user_params)
@user.build_store(store_params)
if @user.save
UserMailer.preregister_confirmation(@user).deliver
sign_in @user
redirect_to prelaunch_gear_path, :flash => {:success => "Welcome"}
else
render root_path
end
end
...
protected
def after_update_path_for(resource)
user_path(resource)
end
private
def user_params
params.require(:user).permit(:firstname, :lastname, :email, :password)
end
def store_params
params.require(:store).permit(:storename)
end
end
您可能會考慮使用toastr來顯示javascript通知。
這是我之前回答過的一個SO問題,可能有助於設置它: Rails 4-烤面包機通知而不是Flash通知 -您只需將Flash通知設置為flash.now[:toastr]
,然后確保調用初始化程序在AJAX請求返回的JS中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.