Where is the best place to implement form validation for an AJAX form in rails when I'm not using a model for the form? And how would I go about implementing it?
My form being submitted needs to check if an IP address exists and is valid before the methods in the controller are run. If the IP is invalid, I'd like to let the user know via a flash[:danger]
message.
views/tools/forms/_ping.html.erb
<%= form_tag ping_tool_path(1), role: "form", class: "form-inline form_ip", name: "ping-form", method: "post", remote: true do %>
<%= text_field_tag :ip, params[:ip], class: "form-control" %>
<%= submit_tag "Ping", name: nil, class: "btn btn-default" %>
<% end %>
views/tools/ping.js.erb
$(".output").html("<%= j (@results) %>");
controllers/tools_controller.rb
def ping
ping_host(params[:ip])
save_host(params[:ip])
# AJAX
respond_to do |format|
format.js
end
end