Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created February 3, 2015 11:03
Show Gist options
  • Save mikecmpbll/f921139a905c026acb00 to your computer and use it in GitHub Desktop.
Save mikecmpbll/f921139a905c026acb00 to your computer and use it in GitHub Desktop.
Multiform setup controller
<%= render "#{@step}_form" %>
class SetupController < ApplicationController
before_action :redirect_if_complete
before_action :set_steps
def create
if params[:back_button]
previous_step
render 'new' and return
end
@setup = Setup.new(@step, strong_params)
@form = @setup.execute_step
next_step if @form.valid?
render 'new'
end
private
def strong_params
params.permit(
:establishment_name,
agencies_form: {
agencies_attributes: [ :id, :name, :_destroy ]
},
categories_form: {
categories_attributes: [ :id, :name, :color, children_attributes: [ :id, :name, :color ] ]
},
user_groups_form: {
user_groups_attributes: [ :id, :name, :_destroy ]
},
users_form: {
users_attributes: [ :id, :name, :_destroy ]
}
)
end
def redirect_if_complete
redirect_to dash_path if Setting.get(:setup_complete)
end
def set_steps
@step_number = session[:setup_step] || 1
@step = Setup::STEPS[@step_number]
@previous_step = Setup::STEPS[@step_number - 1] unless @step_number <= 1
@next_step = Setup::STEPS[@step_number + 1] unless @step_number > Setup::STEPS.keys.max
end
def next_step
session[:setup_step] = @step_number + 1
set_steps
@form = nil
end
def previous_step
session[:setup_step] = @step_number - 1
set_steps
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment