Rails Ajaxified表单不会呈现错误

时间:2018-09-05 16:20:34

标签: ruby-on-rails ajax forms ruby-on-rails-4

我正在尝试在Rails Ajaxified表单上实现简单的唯一性验证。不幸的是,当不接受验证时,不会打印我的验证错误。你能帮我弄清楚为什么吗?

model.rb

class WaitingList < ApplicationRecord
  validates :email, uniqueness: true
end

controller.rb

def create_concours
    @email = params[:email]
    @waiting_user = WaitingList.new(email: @email, source: params[:source], concours_city: params[:choosen_city])
    #First Ajax of Weekendr History snif
    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

_form.html.erb

<%= form_tag concours_path, {id: 'concours-form', remote: true} do %>
                <%= text_field_tag :email,
                    value = nil,
                    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>
                <%= hidden_field_tag :choosen_city, '', id: 'c-choosen-city'%>
                <%= hidden_field_tag 'source', params[:source] %>
                <%= button_tag :submit, class: "landing-form-button" do %>
                    Je tente ma chance !
                <% end %>
              <% end %>

create_concours.js.erb

function refreshForm(innerHTML) {
  const form = document.getElementById("concours-form");
  form.innerHTML = innerHTML;
}

<% if @waiting_user.errors.any? %>
  refreshForm('<%= j render "form" %>');
<% end %>

0 个答案:

没有答案