ruby-on-railswicked-gem

Editing a Wizard multistep form after save - Wicked Gem / Rails


I've been going round in circles all day with this. I have a large multi-step form using the Wicked gem and Ruby on Rails. It works perfectly but I can't figure out how to to get back into the form to edit individual entries.

Iim trying to make the ability to go into the client show page, click an individual client and then from there go back into the quote to edit and update it. As the Wicked gem only seems to work with show and update actions, if I try to build a standard edit action Wicked expects to be on a step therefore doesn't work. I read the I would have to factor the edit action into my show/update actions but I'm having difficulties. Any help would be great thanks!

Clients Controller:

class ClientsController < ApplicationController
before_action :authenticate_user!, only: [:index, :show, :edit]
before_action :set_client, only: [:edit, :show, :update]

def index
    @clients = Client.order('created_at DESC').paginate(page: params[:page], per_page: 10)
end

def show; end

def new
    @client = Client.new 
end

def edit; end

def update
    if @client.update_attributes(client_params)
        redirect_to client_quotes_path
        flash[:success] = 'Client successfully updated'
    else
        render 'edit'
    end
    render_wizard @client
end

# After client is completed:
def create
    @client = Client.new(client_params)
    if @client.valid?
        @client.save
        session[:current_user_id] = @client.id
        ClientMailer.new_client(@client).deliver
        redirect_to quotes_path
    else
        flash[:alert] = 'Sorry, there was a problem with your message. Please contact us directly at ...'
        render :new
    end
end

private

def set_client
    @client = Client.find(params[:id])
end

def client_params
    params.require(:client).permit(:first_name, :last_name, :title, :email, :email_confirmation,
                                   :phone, :time, :reminder, :ref_number, :day, :note, :logs_reminder)
end
end

Quotes Controller:

class QuotesController < ApplicationController
include Wicked::Wizard
before_action :set_client, only: [:show, :update, :quote_success]
steps :profile, :employment, :general_questions, :indemnity_details, :declarations

def show
    @client.build_doctor unless @client.doctor.present?
    @client.build_dentist unless @client.dentist.present?
    @client.old_insurers.build
    @client.practice_addresses.build
    render_wizard
end

def update
    @client.update(client_params)
    render_wizard @client
end

def quote_success; end

private

def set_client
    current_user = Client.find_by_id(session[:current_user_id])
    @client = current_user
end

# After full quote form is completed:
def finish_wizard_path
    if @client.valid?
        ClientMailer.new_quote(@client).deliver
        ClientMailer.new_quote_user_message(@client).deliver
      end
        quote_success_path
    end
end

def client_params
    params.require(:client).permit(:name, :email, :email_confirmation, :phone, :date_required,
                                   :title, :first_name, :last_name, :date_of_birth, :nationality, :reg_body, :reg_date, :reg_type, :reg_number,
                                   :qual_place, :qual_year, :post_grad, :membership ...

Routes:

Rails.application.routes.draw do

devise_for :users

root 'clients#new'

get 'client', to: 'clients#new', as: 'client'
post 'client', to: 'clients#create'

get '/client_quotes', to: 'clients#index', as: 'client_quotes'
get '/client_quotes/:id', to: 'clients#show', as: 'client_quote'
get '/client_quotes/:id/edit', to: 'clients#edit', as: 'edit_client_quote'
patch '/client_quotes/:id', to: 'clients#update'
put '/client_quotes/:id', to: 'clients#update'

resources :quotes, only: [:index, :show, :update, :quote_success]

get 'quote-success' => 'quotes#quote_success'

devise_scope :user do
    get '/login' => 'devise/sessions#new'
end
end

Solution

  • My solution to this in the end was rather than have the edit form as a multi step wizard, I've joined the form data together in a separate view page and got a traditional route to it as you mention. Not perfect but does the job!