如何在Rails 5上的红宝石中创建多步骤表单?

拉吉

我正在尝试在Rails中创建多步骤表单。取而代之的是使用邪恶的宝石(gem)来创建动态表单,我只是使用jquery过渡创建了一个多步骤表单。

new.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => { :id => "msform" }) do |f| %>
  <%= devise_error_messages! %>
  <ul id="progressbar">
    <li class="active">Account Setup</li>
    <li>Social Profiles</li>
    <li>Personal Details</li>
  </ul>
  <fieldset>
    <h2 class="fs-title">Create your account</h2>
    <h3 class="fs-subtitle">This is step 1</h3>
    <%= f.email_field :email, autofocus: true, placeholder: "Email" %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off", placeholder: "Password" %>
    <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "Confirm Password" %>
    <%= button_tag "Next", :type => 'button', :class => "next action-button" %>
  </fieldset>
  <fieldset>
    <h2 class="fs-title">Social Profiles</h2>
    <h3 class="fs-subtitle">Your presence on the social network</h3>
    <%= f.text_field :user_name, autocomplete: "off", placeholder: "Username" %>
    <%= f.text_field :first_name, autocomplete: "off", placeholder: "Firstname" %>
    <%= f.text_field :last_name, autocomplete: "off", placeholder: "Lastname" %>
    <%= f.text_area  :about, autocomplete: "off", placeholder: "Lastname" %>
    <%= button_tag "Previous", :type => 'button', :class => "previous action-button"%>
    <%= button_tag "Next", :type => 'button', :class => "next action-button"%>
  </fieldset>  
  <fieldset>
    <h2 class="fs-title">Personal Details</h2>
    <h3 class="fs-subtitle">We will never sell it</h3>
    <%= f.text_field :profession, autocomplete: "off", placeholder: "Your profession" %>
    <%= f.text_field :skills, autocomplete: "off", placeholder: "Skills" %>
    <%= f.text_field :honours, autocomplete: "off", placeholder: "Hounours" %>
    <%= button_tag "Previous", :type => 'button', :class => "previous action-button"%>
    <%= button_tag "Next", :type => 'button', :class => "next action-button"%>
    <%= f.submit "Sign up", :class => "submit action-button"%>
  </fieldset>
<% end %>    
<%= render "devise/shared/links" %>

我试图使用由ATAKAN Goktepe中创建代码codepen他用HTML,CSS和jQuery。我只是将该html转换为erb,当我使用上述代码时,它在rails中无法正常工作。当我单击下一步按钮时,没有任何反应。

application.js

var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches
console.log("Hello");
$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show();
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({
        'transform': 'scale('+scale+')',
        'position': 'absolute'
      });
            next_fs.css({'left': left, 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".previous").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();


    //de-activate current step on progressbar
    $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

    //show the previous fieldset
    previous_fs.show();
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale previous_fs from 80% to 100%
            scale = 0.8 + (1 - now) * 0.2;
            //2. take current_fs to the right(50%) - from 0%
            left = ((1-now) * 50)+"%";
            //3. increase opacity of previous_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({'left': left});
            previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".submit").click(function(){
    return false;
})

因此,请为我提供此问题的解决方案。jQuery中的代码无法正常工作。提前致谢!

拉贾拉曼

您应该$(document).ready()在文档中触发事件。在您的application.js中尝试一下

var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches
console.log("Hello");
$(document).ready(function(){
$(".next").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show();
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50)+"%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({
        'transform': 'scale('+scale+')',
        'position': 'absolute'
      });
            next_fs.css({'left': left, 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".previous").click(function(){
    if(animating) return false;
    animating = true;

    current_fs = $(this).parent();
    previous_fs = $(this).parent().prev();


    //de-activate current step on progressbar
    $("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");

    //show the previous fieldset
    previous_fs.show();
    //hide the current fieldset with style
    current_fs.animate({opacity: 0}, {
        step: function(now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale previous_fs from 80% to 100%
            scale = 0.8 + (1 - now) * 0.2;
            //2. take current_fs to the right(50%) - from 0%
            left = ((1-now) * 50)+"%";
            //3. increase opacity of previous_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({'left': left});
            previous_fs.css({'transform': 'scale('+scale+')', 'opacity': opacity});
        },
        duration: 800,
        complete: function(){
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

$(".submit").click(function(){
    return false;
})

});

如果您使用的是导轨5和红宝石需要访问turbolinks的功能只是检查出这个

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何创建多步骤表单

来自分类Dev

如何在angualrjs中构建多步骤表单?

来自分类Dev

红宝石宝石如何在Rails环境中工作

来自分类Dev

如何在Rails中传递红宝石中的参数

来自分类Dev

Acumatica:如何在红宝石中创建新客户?

来自分类Dev

从Rails上的表单值红宝石更新模型数据

来自分类Dev

没有向导宝石的Rails多步表单

来自分类Dev

如何在Rails 5中创建API?

来自分类Dev

如何在New Relic上添加新的Rails红宝石应用程序?

来自分类Dev

Cronjob在Rails上的红宝石

来自分类Dev

如何使用React管理多步骤表单?

来自分类Dev

如何在Rails中管理红宝石中的不同时区

来自分类Dev

如何将Angular表单保存到Rails后端的红宝石中?

来自分类Dev

如何创建伪造数据以在Rails Controller上测试我的红宝石

来自分类Dev

如何在Rails中以mb为单位获取红宝石对象的大小?

来自分类Dev

如何在UTC中的过去日期的Rails条件下编写红宝石?

来自分类Dev

如何在Rails 4上有红宝石的模型中包含模块方法?

来自分类Dev

如何在红宝石中运行无礼?

来自分类Dev

如何在红宝石中颠倒顺序

来自分类Dev

在Rails上提交表单红宝石时如何更新表单字段的一部分

来自分类Dev

如何在Rails 4中创建嵌套表单

来自分类Dev

如何在Rails上创建新资源

来自分类Dev

如何在NextJS中以多步骤形式传递数据?

来自分类Dev

如何从多步骤表单向laravel中的一个会话添加数据

来自分类Dev

如何从Node.js中的多步骤表单发布数据并表达到MongoDB?

来自分类Dev

如何在栏杆上使用红宝石以药丸形式显示类别中的文章

来自分类Dev

如何在Rails中从JSON创建对象

来自分类Dev

如何在Rails中创建数组路由

来自分类Dev

如何在Rails中创建任意链接?

Related 相关文章

  1. 1

    如何创建多步骤表单

  2. 2

    如何在angualrjs中构建多步骤表单?

  3. 3

    红宝石宝石如何在Rails环境中工作

  4. 4

    如何在Rails中传递红宝石中的参数

  5. 5

    Acumatica:如何在红宝石中创建新客户?

  6. 6

    从Rails上的表单值红宝石更新模型数据

  7. 7

    没有向导宝石的Rails多步表单

  8. 8

    如何在Rails 5中创建API?

  9. 9

    如何在New Relic上添加新的Rails红宝石应用程序?

  10. 10

    Cronjob在Rails上的红宝石

  11. 11

    如何使用React管理多步骤表单?

  12. 12

    如何在Rails中管理红宝石中的不同时区

  13. 13

    如何将Angular表单保存到Rails后端的红宝石中?

  14. 14

    如何创建伪造数据以在Rails Controller上测试我的红宝石

  15. 15

    如何在Rails中以mb为单位获取红宝石对象的大小?

  16. 16

    如何在UTC中的过去日期的Rails条件下编写红宝石?

  17. 17

    如何在Rails 4上有红宝石的模型中包含模块方法?

  18. 18

    如何在红宝石中运行无礼?

  19. 19

    如何在红宝石中颠倒顺序

  20. 20

    在Rails上提交表单红宝石时如何更新表单字段的一部分

  21. 21

    如何在Rails 4中创建嵌套表单

  22. 22

    如何在Rails上创建新资源

  23. 23

    如何在NextJS中以多步骤形式传递数据?

  24. 24

    如何从多步骤表单向laravel中的一个会话添加数据

  25. 25

    如何从Node.js中的多步骤表单发布数据并表达到MongoDB?

  26. 26

    如何在栏杆上使用红宝石以药丸形式显示类别中的文章

  27. 27

    如何在Rails中从JSON创建对象

  28. 28

    如何在Rails中创建数组路由

  29. 29

    如何在Rails中创建任意链接?

热门标签

归档