Created
April 4, 2017 16:04
-
-
Save alistairtweed/e838778ad62c341a168cfd9d2d9a08ac to your computer and use it in GitHub Desktop.
Remote Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
div id="resource_#{resource.id}"= resource.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h4 New Resource | |
= form_for @resource, remote: true do |f| | |
div | |
= f.label :name | |
= f.text_field :name | |
= f.submit | |
h4 Resources | |
#resources |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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