保存后编辑向导多步骤表单 - Wicked Gem / Rails

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

为了这个我一直在兜圈子。我有一个大型的多步骤表格,使用 Rails 上的 Wicked gem 和 Ruby。它工作得很好,但我不知道如何返回表单来编辑单个条目。

我正在尝试进入客户展示页面,单击一个单独的客户,然后从那里返回报价以编辑和更新它。由于 Wicked gem 似乎只适用于显示和更新操作,如果我尝试构建标准编辑操作,Wicked 期望在一个步骤上因此不起作用。 我读到我必须将编辑操作考虑到我的 show/update 操作中,但我遇到了困难。任何帮助都将非常感谢!

客户端控制器:

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

报价控制器:

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 ...

路线:

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

我最终的解决方案不是将编辑表单作为多步骤向导,而是在单独的视图页面中将表单数据连接在一起,并按照您提到的方式使用传统途径。不完美,但能胜任!

当您更新时,它就像您是 "editing" 元素一样,因此当您想要编辑时需要重定向到向导,而当您调用更新方法时,您实际上是在编辑该条目。故称恶道。