如何在轨道5上的ruby中创建多步骤表单?

时间:2016-11-06 05:20:01

标签: jquery ruby-on-rails ruby

我正在尝试在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中的代码无法正常工作。提前谢谢!

1 个答案:

答案 0 :(得分: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;
})

});

如果您在轨道5上使用ruby并需要访问turbolinks的功能,请查看this