【问题标题】:How to use the Wicked wizard gem with AJAX?如何在 AJAX 中使用 Wicked 向导 gem?
【发布时间】:2017-04-01 07:20:07
【问题描述】:

我正在使用wicked 创建一个多步骤向导。我想使用 AJAX,这样我就可以逐步浏览表单而无需重新加载页面。我在网上找不到有关如何执行此操作的任何信息。有没有人这样做过?

【问题讨论】:

    标签: javascript ruby-on-rails ruby-on-rails-4 formwizard wicked-gem


    【解决方案1】:

    我可能有点晚了,但我只是在做一个使用这些相同功能的项目,所以如果有其他人想知道同样的事情,希望这可能会有所帮助。

    一般来说,最好像设置普通 HTML 向导一样设置 Wicked:

    app/controller/set_up_positions_controller.rb

    class SetUpPositionController < ApplicationController
    
        include Wicked::Wizard
    
        steps :appointment_settings, :lab_settings
    
        def show
            case step
            when :appointment_settings
            when :lab_settings
            end
            render_wizard
        end
    
        def update
            case step
            when :appointment_settings
                #LOGIC
            when :lab_settings
                #LOGIC
            end
            render_wizard @position
        end
    
    end
    

    这里的诀窍是为每个步骤创建一个 step_name.js.erb 和一个 _step_name.html.erb 文件。 IE。

    app/views/set_up_positions/appointment_settings.js.erb

    $('#clinics-content').html('<%= j render "clinic_management/set_up_position/appointment_settings" %>');
    

    app/views/set_up_positions/_appointment_settings.html.erb

    <%= simple_form_for [:clinic_management, @clinic, @position], url: wizard_path, method: :put, remote: true do |f| %>
      <%= f.input :bill_authorization, collection: bill_authorization_options %>
      #etc etc
    <% end%>
    

    您可能希望在 JS 文件中添加一些错误消息(现在它调用相同的 JS 并在字段名称下呈现错误,在这种情况下,这可能是您想要的,也可能不是您想要的)。

    根据您的用例,使用其中一个 JS 框架来处理这类事情(如 steps.js)也可能更容易。我没有,因为我比 JS 更了解 ruby​​,但对于其他人来说,这也是一种选择。

    【讨论】:

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