Skip to content

Instantly share code, notes, and snippets.

@alistairtweed
Created April 4, 2017 16:04
Show Gist options
  • Save alistairtweed/e838778ad62c341a168cfd9d2d9a08ac to your computer and use it in GitHub Desktop.
Save alistairtweed/e838778ad62c341a168cfd9d2d9a08ac to your computer and use it in GitHub Desktop.
Remote Forms
div id="resource_#{resource.id}"= resource.name
<% if @resource.errors.any? %>
console.log("Resource could not be created.");
<% else %>
$('#new_resource')[0].reset();
$('#resources').append('<%= j render(@resource) %>');
$('#resource_<%= @resource.id %>').effect("highlight", {}, 3000).submitOnCheck();
<% end %>
h4 New Resource
= form_for @resource, remote: true do |f|
div
= f.label :name
= f.text_field :name
= f.submit
h4 Resources
#resources
class ResourcesController < ApplicationController
def create
@resource = Resource.new(permitted_params)
respond_to do |format|
if @resource.save
format.html { redirect_to resources_url, notice: "Resource was successfully created." }
format.js
else
format.html do
flash.now[:error] = "Resource could not be created."
render :new
end
format.js
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment