【问题标题】:Editing a Wizard multistep form after save - Wicked Gem / Rails保存后编辑向导多步骤表单 - Wicked Gem / Rails
【发布时间】:2017-03-27 19:07:19
【问题描述】:

我整天都在兜圈子。我有一个使用 Wicked gem 和 Ruby on Rails 的大型多步骤表单。它工作得很好,但我不知道如何回到表单来编辑单个条目。

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

客户端控制器:

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

【问题讨论】:

  • 您是否考虑过为邪恶的向导单独路由,以及在值被持久化后使用更传统的路由?
  • 这实际上是我最终所做的,而不是将编辑表单作为一个多步骤向导,我将表单数据连接在一起并获得了您提到的传统路线。不完美,但可以完成工作!

标签: ruby-on-rails wicked-gem


【解决方案1】:

我最终对此的解决方案不是将编辑表单作为一个多步骤向导,而是将表单数据合并到一个单独的视图页面中,并获得了您提到的传统路线。不完美,但能胜任!

【讨论】:

    【解决方案2】:

    当你更新它就像你在“编辑”元素一样,所以当你想编辑时你需要重定向到向导,当你调用更新方法时你真的会编辑那个条目。所以叫恶道吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多