Skip to content

Instantly share code, notes, and snippets.

@ksugiarto
Last active December 4, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksugiarto/26df633ca70a0e6453d0 to your computer and use it in GitHub Desktop.
Save ksugiarto/26df633ca70a0e6453d0 to your computer and use it in GitHub Desktop.
Rails AJAX Form Usage
<%= form_for(@category, :html => { :class => "form-horizontal" }, :remote => true) do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
<ul>
<% @category.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :name, :class => "col-sm-4 control-label" %>
<div class="col-sm-6"><%= f.text_field :name, :required => true, :class => "form-control" %></div>
</div>
<div class="form-group">
<%= f.label :description, :class => "col-sm-4 control-label" %>
<div class="col-sm-6"><%= f.text_area :description, :rows => 3, :class => "form-control" %></div>
</div>
<div class="form-group">
<div class="col-sm-4"></div>
<div class="col-sm-6"><%= f.submit "Submit", :class => "btn btn-success", :disable_with => "Processing" %></div>
</div>
<% end %>
class CategoriesController < ApplicationController
def get_data
@categories = Category.order(:name)
end
# GET /categories/new
# GET /categories/new.json
def new
@category = Category.new
# if you are just using one type of responding file, for example just .js.erb then you don't have to specify the respond_to block
# rails controller will automatically search the only one 'new.*' named file.
end
# POST /categories
# POST /categories.json
def create
@category = Category.create(params[:category])
get_data
# if you are just using one type of responding file, for example just .js.erb then you don't have to specify the respond_to block
# rails controller will automatically search the only one 'create.*' named file.
end
end
$("#table").html("<%= escape_javascript(render('table')) %>");
$('#general_modal').modal("hide");
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-8">
<h4 align="center">Category</h4>
</div>
<div class="col-md-2" align="right">
<%= link_to new_category_path, :class => "btn btn-default btn-sm", :remote => true do %>
<span class="glyphicon glyphicon-plus-sign"></span>&nbsp;New
<% end %>
</div>
</div>
<div id="table"><%= render 'table' %></div>
<!-- Modal -->
<div id="general_modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="BranchModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title"></h3>
</div>
<div class="modal-body">
</div>
</div>
</div>
</div>
$('#general_modal').modal('show');
$('#general_modal .modal-title').html("Category - New");
$('#general_modal .modal-body').html("<%= escape_javascript(render 'form') %>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment