Last active
December 4, 2015 14:17
-
-
Save ksugiarto/26df633ca70a0e6453d0 to your computer and use it in GitHub Desktop.
Rails AJAX Form Usage
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
<%= 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 %> |
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 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 |
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
$("#table").html("<%= escape_javascript(render('table')) %>"); | |
$('#general_modal').modal("hide"); |
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 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> 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> |
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
$('#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