Last active
April 13, 2021 15:55
-
-
Save artem-d/7fe9b0d81583abd04512 to your computer and use it in GitHub Desktop.
ActiveAdmin quick add and quick edit
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
# http://helabs.com.br/blog/2014/05/28/quick-add-and-quick-edit-on-active-admin/ | |
# room.rb | |
action_item only: :index do | |
link_to 'Quick add', admin_room_quick_add_path, class: 'fancybox', data: { 'fancybox-type' => 'ajax' } | |
end | |
controller do | |
def quick_add | |
@room = Room.new | |
render layout: false | |
end | |
def quick_create | |
@room = Room.new(permitted_params[:room]) | |
@room.save | |
render 'quick_response', layout: false | |
end | |
def quick_edit | |
@room = Room.find(params[:id]) | |
render layout: false | |
end | |
def quick_update | |
@room = Room.find(params[:id]) | |
@room.update(permitted_params[:room]) | |
render 'quick_response', layout: false | |
end | |
end | |
# ... | |
index do | |
# ... | |
actions defaults: true do |room| | |
link_to 'Quick Edit', admin_room_quick_edit_path(room), class: 'fancybox', data: { 'fancybox-type' => 'ajax' } | |
end | |
end | |
# views/admin/rooms/quick_add.html.haml | |
#modal | |
%h2 New Room | |
%p.modal-description Room Quick Add. | |
.section#quick-errors | |
-# # = render 'error_messages', object: @room | |
.section.form-fluid | |
= simple_form_for(@room, url: admin_room_quick_create_path, remote: true) do |f| | |
.form-inputs | |
= f.input :apartment | |
= f.input :rm_num | |
= f.input :area | |
= f.input :status, as: :select, collection: Room.statuses.keys, include_blank: false | |
= f.input :bed_size | |
= f.input :window | |
= f.input :price | |
= f.input :properties | |
%ul.form-actions | |
%li | |
= f.submit 'Save' | |
# views/admin/rooms/quick_edit.html.haml | |
#modal | |
%h2 Quick Edit Room | |
%p.modal-description Edit Room Budget. | |
.section#quick-errors | |
-# # = render 'error_messages', object: @room | |
.section.form-fluid | |
= simple_form_for(@room, url: admin_room_quick_update_path, remote: true) do |f| | |
.form-inputs | |
= f.input :apartment | |
= f.input :rm_num | |
= f.input :area | |
= f.input :status, as: :select, collection: Room.statuses.keys, include_blank: false | |
= f.input :bed_size | |
= f.input :window | |
= f.input :price | |
= f.input :properties | |
%ul.form-actions | |
%li | |
= f.submit 'Save' | |
# views/admin/rooms/quick_response.js.erb | |
<% if @room.errors.any? %> | |
var html = "<%= escape_javascript(render 'error_messages', object: @room) %>"; | |
$('#quick-add-errors').html(html); | |
<% else %> | |
window.location.reload(); | |
<% end %> | |
# active_admin.js.coffee | |
#= require fancybox | |
$ -> | |
$('a.fancybox').fancybox() | |
# app/assets/stylesheets/active_admin.css.scss: | |
@import "active_admin/mixins"; | |
@import "active_admin/base"; | |
@import "fancybox"; | |
# routes.rb | |
get '/admin/rooms/new/quick_add' => 'admin/rooms#quick_add', as: :admin_room_quick_add | |
post '/admin/rooms/quick_create' => 'admin/rooms#quick_create', as: :admin_room_quick_create | |
get '/admin/rooms/:id/edit/quick_edit' => 'admin/rooms#quick_edit', as: :admin_room_quick_edit | |
patch '/admin/rooms/:id/quick_update' => 'admin/rooms#quick_update', as: :admin_room_quick_update | |
# Gemfile | |
gem 'fancybox2-rails' | |
gem 'simple_form' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment