rails登录表单从标题向下滑动,当登录尝试失败时,如何保持登录表单打开?

时间:2016-04-29 07:13:25

标签: jquery ruby-on-rails

如果登录尝试失败,redirect_to:back只会重新加载上一页但不会继续显示dropdrown表单。

编辑:

form_for guide建议使用Ajax:

5.2处理Ajax 与其他表单不同,创建异步文件上载表单并不像使用remote:true提供form_for那么简单。“

这意味着remote:true是Rails内置的Ajax?

如何正确添加remote:true到我的表单?

编辑:

This question might be helpful for those trying to solve these issues.

会话控制器

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      if user.activated?
        log_in user
        params[:session][:remember_me] == '1' ? remember(user) : forget(user)
        redirect_back_or user
      else
        message  = "Account not activated. "
        message += "Check your email for the activation link."
        flash[:warning] = message
        redirect_to root_url
      end
    else
      flash.now[:danger] = 'Invalid email/password combination'         
      redirect_to :back 
    end
  end

  def destroy
    log_out if logged_in?
    redirect_to root_url
  end
end

Jquery的

$(document).on('page:change', function () {
    $('.sessions').hide();
    $('li.close-header').hide();
    $('li.signin').on('click', function() {
    $('li.signin').fadeOut(100);
    $('.sessions').slideDown(500);
    $('li.close-header').fadeIn(100);
    $('li.close-header').on('click', function() {
    $('.sessions').slideUp(500); 
    $('li.close-header').fadeOut(100);
    $('li.signin').fadeIn(100);
  }); 
  });  
});

表格

<div class="sessions">
<h1>Sign Up</h1>
<div class="row">
<div class="col-md-3">
  <%= form_for(:session, url: login_path) do |f| %>

    <%= f.label :email %>
    <%= f.email_field :email, class: 'form-control' %>

    <%= f.label :password %>
    <%= link_to "(forgot password)", new_password_reset_path %>
    <%= f.password_field :password, class: 'form-control' %>

    <%= f.label :remember_me, class: "checkbox inline" do %>
      <%= f.check_box :remember_me %>
      <span>Remember me on this computer</span>
    <% end %>

    <%= f.submit "Log in", class: "btn btn-primary sessions" %>
  <% end %>

   <p>New user? <%= link_to "Sign up now!", signup_path %> </p>         
</div>

1 个答案:

答案 0 :(得分:0)

将remote:true添加到表单以使用ajax处理它并创建create.js.erb文件并在视图中呈现错误。还考虑添加客户端验证以配合服务器端验证。