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

时间:2017-03-27 19:07:20

标签: ruby-on-rails wicked-gem

我整天都围着这个圈子走了一圈。我有一个使用Wicked gem和Ruby on Rails的大型多步骤表单。它工作得很好,但我无法弄清楚如何回到表单来编辑单个条目。

Iim尝试进入客户端显示页面,单击一个单独的客户端,然后从那里返回到报价进行编辑和更新。由于Wicked gem似乎只与show和update动作一起使用,如果我尝试构建一个标准的编辑动作,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

2 个答案:

答案 0 :(得分:0)

我最终的解决方案是将编辑表单作为一个多步骤向导,而不是将表单数据加入到一个单独的视图页面中,并按照您的提及获得传统路径。不完美,但做得好!

答案 1 :(得分:0)

当你更新时,就像你是&#34;编辑&#34;元素,所以当你想编辑时需要重定向到向导,当你调用更新方法时,你真的要编辑那个条目。所以称之为邪恶的道路。