From 5f514bfb4ccb3a14d4c2ce2b3f20a69d7eabc3c3 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Thu, 6 Apr 2017 01:41:16 +0530 Subject: [PATCH] Improve remove creation using Ajax --- app/controllers/admin/rooms_controller.rb | 14 ++++++++------ app/views/admin/rooms/_form.html.haml | 6 +++--- app/views/admin/rooms/create.js.erb | 2 ++ app/views/layouts/_admin.html.haml | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 app/views/admin/rooms/create.js.erb diff --git a/app/controllers/admin/rooms_controller.rb b/app/controllers/admin/rooms_controller.rb index da7dfc57..26257f45 100644 --- a/app/controllers/admin/rooms_controller.rb +++ b/app/controllers/admin/rooms_controller.rb @@ -3,6 +3,7 @@ module Admin load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource :venue, through: :conference, singleton: true load_and_authorize_resource through: :venue + after_action :prepare_unobtrusive_flash, only: [:create] def index; end @@ -14,12 +15,13 @@ module Admin def create @room = @venue.rooms.new(room_params) - if @room.save - redirect_to admin_conference_venue_rooms_path(conference_id: @conference.short_title), - notice: 'Room successfully created.' - else - flash.now[:error] = "Creating Room failed: #{@room.errors.full_messages.join('. ')}." - render :new + respond_to do |format| + if @room.save + flash.now[:notice] = 'Room successfully created.' + else + flash.now[:error] = "Creating Room failed: #{@room.errors.full_messages.join('. ')}." + end + format.js end end diff --git a/app/views/admin/rooms/_form.html.haml b/app/views/admin/rooms/_form.html.haml index 8e515290..71332a78 100644 --- a/app/views/admin/rooms/_form.html.haml +++ b/app/views/admin/rooms/_form.html.haml @@ -8,8 +8,8 @@ = @room.name .row .col-md-8 - = semantic_form_for(@room, url: (@room.new_record? ? admin_conference_venue_rooms_path : admin_conference_venue_room_path(@conference.short_title, @room))) do |f| - = f.input :name, input_html: { autofocus: true} - = f.input :size, input_html: {size: 5} + = semantic_form_for(@room, url: (@room.new_record? ? admin_conference_venue_rooms_path : admin_conference_venue_room_path(@conference.short_title, @room)), remote: (true if @room.new_record?)) do |f| + = f.input :name, input_html: { autofocus: true}, id: 'room_name' + = f.input :size, input_html: {size: 5}, id: 'room_size' %p.text-right = f.action :submit, as: :button, button_html: { class: 'btn btn-primary' } diff --git a/app/views/admin/rooms/create.js.erb b/app/views/admin/rooms/create.js.erb new file mode 100644 index 00000000..b4b9eaee --- /dev/null +++ b/app/views/admin/rooms/create.js.erb @@ -0,0 +1,2 @@ +$('#room_name').val(''); +$('#room_size').val(''); diff --git a/app/views/layouts/_admin.html.haml b/app/views/layouts/_admin.html.haml index a9add1ac..ad289d44 100644 --- a/app/views/layouts/_admin.html.haml +++ b/app/views/layouts/_admin.html.haml @@ -8,7 +8,7 @@ -# Index admin sidebar = render 'layouts/admin_sidebar_index' .col-md-10 - #messages + #messages.unobtrusive-flash-container =render 'layouts/messages' #content - = yield \ No newline at end of file + = yield