【问题标题】:How to create a multi step form in ruby on rails 5?如何在 ruby​​ on rails 5 中创建多步骤表单?
【发布时间】:2017-03-19 16:19:09
【问题描述】:

我正在尝试在 Rails 中创建多步表单。而是使用 wicked like 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 中的代码无法正常工作。提前致谢!

【问题讨论】:

  • 您是否打开了 JavaScript 控制台并查找错误?此外,您似乎没有将 javascript 包装在准备好的文档中 ($(function(){ ... &lt;all the javascript here&gt; ...}))

标签: jquery ruby-on-rails ruby


【解决方案1】:

您应该使用$(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;
})

});

如果您在 rails 5 上使用 ruby​​ 并且需要访问 turbolinks 的功能,请查看this。

【讨论】:

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