我有一个SimpleForm设置,验证效果很好。我现在想实现AJAX,所以我不需要执行页面重新加载。当我将:remote => true
添加到我的代码中时,验证不再有效。这是预期的行为吗?当remote设置为true时,我的SimpleForm验证是否不再有效?
以下是我的代码示例:
<%= simple_form_for @business, :url => {:action => "create", :id => "submit_subscription_signup_form"}, :html => {:class => "form-horizontal"}, :remote => true do |f| %>
<% if notification = f.error_notification %>
<div class="alert alert-error fade in">
<a class="close" data-dismiss="alert" href="#">×</a>
<%= notification %>
</div>
<% end %>
<%= f.input :business_name, :input_html => { :class => "span6" } %>
<%= f.input :address1, :input_html => { :class => "span6" } %>
<%= f.input :city, :input_html => { :class => "span6" } %>
<div class="actions" style="text-align: right;"><%= f.submit "Sign Up", :class => 'btn btn-success', :id => "signup_form_submit_button" %></div>
<% end %>
我在控制器中创建动作:
def create
@business = Business.new(params[:business])
if @business.save
#removed logic for simplicity
respond_to do |format|
format.html
format.js { render 'subscriptions/_logged_in' }
end
else
respond_to do |format|
format.html { render 'index' }
format.js { render 'subscriptions/_signup' }
end
end
end
看到我希望提交失败并显示验证,然后我的_signup.js.erb部分将被调用。主视图包含许多通过jQuery显示或隐藏的表单。所以_signup.js.erb包含:
$('#login_form').hide()
$('#logged_in_form').hide()
$('#signup_form').show()