如果未通过验证,我正在尝试使用Ajax在表单上呈现错误。不幸的是,我的表单再次被渲染,但是没有错误提示。你能帮我弄清楚为什么吗?
_concours_form.html.erb
<%= simple_form_for @waiting_user, {id: 'concours-form', url: concours_path, remote: true} do |f| %>
<%= f.text_field :email,
class: "landing-form-input",
autocomplete: "off",
required: true,
pattern: '[^@]+@[^@]+\.[a-zA-Z]{2,6}',
placeholder: "Renseignez ton email ici"
%>
<i class="fa fa-envelope"></i>
<%= f.hidden_field :concours_city,
value:'',
id: 'c-choosen-city'
%>
<%= f.hidden_field :source,
value: params[:source]
%>
<%= f.button :submit,
class: "landing-form-button",
value: "Je tente ma chance !"
%>
<% end %>
pages_controller.rb
def create_concours
@email = waiting_list_params[:email]
@waiting_user = WaitingList.new(waiting_list_params)
respond_to do |format|
if @waiting_user.save
UserMailer.welcome(@email).deliver_now
format.html { redirect_to concours_path }
format.js
else
format.html
format.js
end
end
end
private
def waiting_list_params
params.require(:waiting_list).permit(:email, :concours_city, :source)
end
create_concours.js.erb
<% if @waiting_user.errors.any? %>
refreshForm('<%= j render "pages/concours_form" %>');
<% end %>