Topic: Resetting new form on create (ajax)
Hello! I have an app that displays business hours on index action of page controller (home page). You can create, edit and destroy a business_hour through ajax. The issue I am having is that I can not figure out how to have the "new" form reset after a new record is corrected. When I open the form again, it's set at the last records settings and not the default.
Here is some of my code. Hopefully you can follow it.
pages_controller.rb
class PagesController < ApplicationController
def index
@business_hour = BusinessHour.new
@business_hours = BusinessHour.order(:day)
end
endindex.html.slim
/! Show Business Hours
#business-hours.panel.four.columns
h4 Business Hours
#business-hours-list
= render :partial => 'business_hours/show_list', :locals => {:business_hours => @business_hours}
/! Modals
#modals
- if !@business_hours.empty?
/! Edit Business Hour Modals
- @business_hours.each do |business_hour|
= render :partial => 'business_hours/edit', :locals => {:business_hour => business_hour, :action => 'edit'}
- if !@business_hours.empty?
/! Destroy Business Hour Modals
- @business_hours.each do |business_hour|
= render :partial => 'business_hours/destroy', :locals => {:business_hour => business_hour}
/! New Business Hour Modal
= render :partial => 'business_hours/new', :locals => {:business_hour => @business_hour, :action => 'new'}_new.html.slim
.reveal-modal id='newBusinessHour'
h2 New Business Day
= render :partial => 'business_hours/form', :locals => {:business_hour => @business_hour, :action => 'new'}
a.close-reveal-modal ×_form.html.slim
= form_for business_hour, remote: true do |f|
.field
- if (defined? action) && (action == 'new')
= f.select :day, Date::DAYNAMES.zip((0..6).to_a)
= f.label :start_time
= f.time_select :start_time,
:combined => true,
:default => Time.now.change(:hour => 9, :min => 00),
:minute_interval => 15,
:time_separator => "",
:start_hour => 9,
:start_minute => 00,
:end_hour => 21,
:end_minute => 00
= f.label :end_time
= f.time_select :end_time,
:combined => true,
:default => Time.now.change(:hour => 21, :min => 00),
:minute_interval => 15,
:time_separator => "",
:start_hour => 9,
:start_minute => 00,
:end_hour => 21,
:end_minute => 00
.field
= f.check_box :closed
.actions = f.submit 'Save'business_hours_controller.rb
class BusinessHoursController < ApplicationController
# POST /business_hours
def create
params[:business_hour].parse_time_select! :start_time
params[:business_hour].parse_time_select! :end_time
@business_hour = BusinessHour.new(params[:business_hour])
respond_to do |format|
if @business_hour.save
format.js {
@business_hours = BusinessHour.order(:day)
}
else
format.js { render 'reload', :locals => { :action => 'new' } }
end
end
end
# PUT /business_hours/
def update
params[:business_hour].parse_time_select! :start_time
params[:business_hour].parse_time_select! :end_time
@business_hour = BusinessHour.find(params[:id])
respond_to do |format|
if @business_hour.update_attributes(params[:business_hour])
format.js { @business_hours = BusinessHour.order(:day) }
else
format.js { render 'reload', :locals => { :action => 'edit' } }
end
end
end
# DELETE /business_hours/1
def destroy
@business_hour = BusinessHour.find(params[:id])
@business_hour.destroy
respond_to do |format|
format.js { @business_hours = BusinessHour.order(:day) }
end
end
endcreate.js.coffee
$('#business-hours-list').html "<%= j render :partial => 'show_list', :locals => {:business_hours => @business_hours} %>"
$('#modals').append "<%= j render :partial => 'edit', :locals => {:business_hour => @business_hour, :action => 'edit'} %>"
$('#modals').append "<%= j render :partial => 'destroy', :locals => {:business_hour => @business_hour} %>"
$('.reveal-modal#newBusinessHour').trigger 'reveal:close'
$('#newBusinessHour').remove()
$('#modals').append "<%= j render :partial => 'new', :locals => {:business_hour => @business_hour} %>"It's a lot to take in. Basically, I am using modals to create, edit and destroy a business hour (simple monday through friday with open and close times). The modals are rendered on the index page. The remote form calls the business_controller and renders js. The javascript then updates the business hours list, adds or removes appropriate modals, and closes the current open modal.
I just can't figure out how to get the new modal to "reset". I need to pass @business_hours to create.js.coffee so it can create the appropriate modals. I've tried also passing @new_business_hour = BusinessHour.new to create.js.coffee and having that passed to the form partial. Same results.
I am sure there is something stupid I am not doing. I really appreciate your help. Thank you in advanced.